On Wed, Apr 21, 2010 at 04:38:34PM +0200, slyght wrote: > Hi there, > > I recently had an issue with xmlAddChild and xmlCopyNode which I solved but > still try to understand. > > What I did was creating a new subtree and afterwards inserting it into an > existing tree. > So after creating a new node and some children I inserted it into the > existing tree via: > tmpNodePtr = xmlAddChild(parentNodePtr, xmlCopyNode(newNodePtr, 1)); > > I used xmlCopyNode because the nodes where newly declared within that > function and I assumed the pointers would be invalid as soon as I leave that > function. > > The real issue I had was that the namespace of the new node got lost due to > the copy (tmpNodePtr->ns is NULL). I don't understand why, since I have > given the option "1" which should do a recursive copy including namespaces. > > So now that I removed the xmlCopyNode and instead wrote: > xmlAddChild(parentNodePtr, newNodePtr); > everything is fine. > > Could someone help me to understand why xmlCopyNode is not working in the > way I assumed? > > By the way: The existing tree and the new subtree should have the same > namespace, so maybe there's a more clever way to do what I want?
namespace references are just pointers on elements or attributes to the actual namespace declaration higher up in the tree. If you copy and the namespace is not part of that subtree you're likely to loose it indeed. My advice is to not build the subtree in isolation and then graft it to the target but instead always build it in its context, i.e. passing the target to the subtree creation. There is contextual references from a subtree like namespaces and entities, and it's best to avoid moving stuff if you don't understand perfectly all the data structures involved. Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ dan...@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/ _______________________________________________ xml mailing list, project page http://xmlsoft.org/ xml@gnome.org http://mail.gnome.org/mailman/listinfo/xml