Reputation: 571
I have a document like this
<a:root xmlns:a="http://example.com/roots" xmlns:b="http://example.com/subs" xmlns:c="http://example.com/subsubs">
<b:sub>
<c:subsub>Hello World</c:subsub>
</b:sub>
</a:root>
Using xmlNodeDump I want to output XML for sub and deeper only i.e. I want to end up with
<b:sub xmlns:b="http://example.com/subs" xmlns:c="http://example.com/subsubs">
<c:subsub>Hellow World<c:subsub>
</b:sub>
However because the namespace declarations are in the root tag, when I dump the XML, the namespace declarations are lost and I end up with
<b:sub>
<c:subsub>Hellow World<c:subsub>
</b:sub>
Which is not valid anymore. The question is: How can I make sure the XML being output has the relevant namespace declarations added to the new top-level element i.e. to sub ?
Upvotes: 2
Views: 408
Reputation: 33648
The trick is to copy the node before dumping it:
xmlNodePtr copy = xmlCopyNode(node, 1);
xmlNodeDump(..., copy, ...);
xmlFreeNode(copy);
xmlCopyNode
adds the necessary namespace declarations to the copied node.
Upvotes: 4