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

Mike Olson Mike.Olson@fourthought.com
18 Jun 2002 15:32:31 -0600


On Tue, 2002-06-18 at 15:30, Douglas Bates wrote:
> 
> 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)

Right here, you are destroying the list as you walk over it so you end
up skipping items.

1.  Start loop with what is at index 0
2.  Remove the firs thing from the list (with the append child)
3.  Start loop with index of 1 (in the original list this will be the
third item)
etc.

try this
for n in nd.childNodes[:]: rec.appendChild(n)


Mike

> 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/
> 
> 
> _______________________________________________
> XML-SIG maillist  -  XML-SIG@python.org
> http://mail.python.org/mailman/listinfo/xml-sig
-- 
Mike Olson                                Principal Consultant
mike.olson@fourthought.com                +1 303 583 9900 x 102
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St,                      http://4Suite.org
Boulder, CO 80301-2537, USA
XML strategy, XML tools, knowledge management