Re: [xml] Reparenting nodes across documents?

On Tue, Aug 03, 2010 at 11:10:24AM -0400, Marco Rogers wrote:
I'm using manipulation functions like xmlAddChild and xmlAddNextSibiling in
my javascript binding library (  I
whenever I use these I have a function that checks whether the incoming new
node is already a child of a different document and if so it does a node
copy and destroys the old node.  I've now been told that this isn't
necessary because functions like xmlAddChild already do this check and will
do the document transfer for me.  Is this correct?  What other things should
I take into account when transferring nodes across documents?

  It's more complex than that...
Basically a subtree is linked  to the main document by multiple things:

  - the parent and doc pointers which are handled by xmlAddChild and
    the like
  - entity references will point to definitions in the DTD if used and
    not replaced at parsing time
  - the namespaces, that's far more complex, some element or attribute
    in the subtree may references namespaces defined in upper layer
    in the tree, that need to be carefully fixed, changing those
    pointers to equivalent namespaces definitions in the target
    xmlReconciliateNs() tries to handle this too.
  - dictionary, some strings like Names or short text strings are
    usually allocated from the document dictionnary at parse time
    unless you deactivate them, so if the source and target documents do
    not share the same dictionaries, this will lead to crash at the time
    the tree is freed, either disable dictionary use at parse time
    or make sure the documents share the same ditionnary

in general, even if moving subtrees from one document to
another sounds simple this is actually a very complex operation to
do right in all cases.


Daniel Veillard      | libxml Gnome XML XSLT toolkit
daniel veillard com  | Rpmfind RPM search engine | virtualization library

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]