[XML-SIG] Moving DOM node hierarchies

Alexandre Fayolle alf@logilab.com
Wed, 11 Oct 2000 12:38:15 +0200 (CEST)

On Tue, 10 Oct 2000, Clarence Gardner wrote:

> Thanks.  I read the references, but my posting was more oriented toward
> why the insertion was disallowed in the first place.  Of course, the
> people writing the specs put a lot more thought into these things than
> I do, but I would think this would come up quite often and they might
> have rationalized it.  I was particularly amazed, after coming across
> this, that DocumentFragments couldn't even be inserted.  

They can be inserted. The DOM core spec says: 

insertBefore :
Inserts the node newChild before the existing child node refChild. If
refChild is null, insert newChild at the end of the list of children.  If
newChild is a DocumentFragment object, all of its children are inserted,
in the same order, before refChild. If the newChild is already in the
tree, it is first removed.

DOM3 should provide facilities for moving nodes across documents. In the
meanwhile, you have to use Document.importNode(node,deep_copy=true) before
inserting the new copy in the tree. If you really want to mimick DOM2
behaviour, you also have to manually remove the original node from the
first document.

So this gives something like 

def appendFromOtherDoc(node1,node2):
    imported = node2.ownerDocument.importNode(node1,1)

    # optionnally with 4DOM, you may want to remove circular references
    from xml.dom.ext import ReleaseNode

Notice that this does not answer your question on why this is done the way
it is done. I'm sometimes as baffled as you are. My biggest grudge is Why
non qualified attributes do not inherit the element default namespace ?

Alexandre Fayolle
http://www.logilab.com - "Mais oł est donc Ornicar ?" - 
LOGILAB, Paris (France).