This afternoon at work, I came across something most odd with XML::LibXML: When you call
removeChild() on an element, it loses its namespace declarations. For example, if I have this XML:
<foo> <bar:bar xmlns:bar="http://example.com/" /> </foo>
Then, I call
removeChild() on the <bar:bar> element, immediately followed by
$node->toString(). And this is what I get back:
Which is not well formed XML because the namespace declaration is missing. Somewhere along the line, the declaration is being lost. I suspect that it should be copied in to the $node when it’s removed from its parent document, but that’s not happening. As to whether this is a bug in XML::LibXML or libxml2, I don’t know. I’ll file a report and we’ll see what happens.
Update: how odd. This minimal test case works as expected. I’ll have to examine the original situation more closely to work out what I’m not reproducing…
Update#2: I got the example wrong. Actually, the source xml looked like this:
<foo xmlns:bar="http://example.com/"> <bar:bar /> </foo>
This fails as expected.