[XML-SIG] Deep copy of an element from one document to another

Douglas Bates bates@stat.wisc.edu
18 Jun 2002 16:30:05 -0500


Using Python 2.1 or 2.2 and the recently build Debian python2.2-xml
(0.7.1-1) package, I get weird behaviour when I try to copy an element
from one document into another.  The test script is

#!/usr/bin/env python2.2 
import xml.dom
from xml.xpath import Evaluate
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
import os.path, re

dom = PyExpat.Reader().fromUri('/tmp/foo.xml')
od = xml.dom.DOMImplementation.implementation.createDocument(xml.dom.EMPTY_NAMESPACE, 'CISrecords', None)
dc = Evaluate('ListRecords/record/metadata/dc', dom).pop()
desc = Evaluate('description', dc).pop()
nd = od.importNode(desc, 1)
print nd.childNodes
rec = od.documentElement
for n in nd.childNodes: rec.appendChild(n)
print rec.childNodes
print nd.childNodes


Basically I am doing a deep copy of the description node as nd then
appending the children of nd to the rec node.  The output shows that
nd consists of both Text Nodes and Element Nodes but only the Text Nodes
are being appended to rec.

<NodeList at 830f094: [<Text Node at 8319fbc: u'I. Richard Savage wa...'>, <Element Node at 831d934: Name='i' with 0 attributes and 1 children>, <Text Node at 830a34c: u' (1968, as Editor of...'>, <Element Node at 8198ed4: Name='i' with 0 attributes and 1 children>, <Text Node at 830eabc: u' (1974\xe21977), and as...'>, <Element Node at 830c0dc: Name='i' with 0 attributes and 1 children>, <Text Node at 830b83c: u', edited by Bruce Sp...'>]>
<NodeList at 83035a4: [<Text Node at 8319fbc: u'I. Richard Savage wa...'>, <Text Node at 830a34c: u' (1968, as Editor of...'>, <Text Node at 830eabc: u' (1974\xe21977), and as...'>, <Text Node at 830b83c: u', edited by Bruce Sp...'>]>
<NodeList at 830f094: [<Element Node at 831d934: Name='i' with 0 attributes and 1 children>, <Element Node at 8198ed4: Name='i' with 0 attributes and 1 children>, <Element Node at 830c0dc: Name='i' with 0 attributes and 1 children>]>


Should I expect this to happen?  I don't see anything in the
documentation that would indicate that appendNode would not append an
Element Node.

Is there a better way of doing this?

-- 
Douglas Bates                            bates@stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/