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

Uche Ogbuji uche.ogbuji@fourthought.com
Wed, 19 Jun 2002 08:43:50 -0600


> 
> Mike Olson writes:
>  > 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)
> 
> Another idiom that I like is this:
> 
>     n = nd.firstChild
>     while n is not None:
>         next = n.nextSibling
>         rec.appendChild(n)
>         n = next
> 
> This has the advantage of not creating a separate list.  It is,
> unfortunately, more verbose.
> 
> Perhaps Python DOMs should implement the iterator protocol:
> 
> def _childiter(node):
>     n = node.firstChild
>     while n is not None:
>         next = n.nextSibling
>         yeild n
>         n = next
> 
> class Node:
>     ...
>     def __iter__(self):
>         return _childiter(self)

I like this idea, except iterators are 2.2 only.  I suppose the __iter__ 
function would be harmless in an older version, but it puts the onus on the 
user to write his code portably, probably restoring the verbosity the iterator 
would have minimized.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Track chair, XML/Web Services One (San Jose, Boston): 
http://www.xmlconference.com/
DAML Reference - http://www.xml.com/pub/a/2002/05/01/damlref.html
The Languages of the Semantic Web - http://www.newarchitectmag.com/documents/s=
2453/new1020218556549/index.html
XML, The Model Driven Architecture, and RDF @ XML Europe - 
http://www.xmleurope.com/2002/kttrack.asp#themodel