[lxml-dev] Behaviour of Element.iter() changes when removing a node depending on whether the node has children or not
Geoffrey Sneddon
foolistbar at googlemail.com
Sat Jul 5 20:13:43 CEST 2008
Hi (and sorry for the stupidly long subject, but otherwise it doesn't
actually cover the subject),
I'm not sure whether this is a bug or not (if not, there probably
ought to be some note in the docs), but, for example:
>>> from lxml import etree
>>> foo = etree.fromstring("<root><a><b/></a><a/></root>")
>>> for element in foo.iter(etree.Element):
... print element.tag
... if element.tag == "a":
... element.getparent().remove(element)
...
root
a
b
>>> foo = etree.fromstring("<root><a/><a/></root>")
>>> for element in foo.iter(etree.Element):
... print element.tag
... if element.tag == "a":
... element.getparent().remove(element)
...
root
a
a
Only in the latter case does the final a element actually appear in
the iteration, whereas in the former case it just vanishes. What
changes the behaviour is the fact that in former case the first a
element has a child: an empty b element.
--
Geoffrey Sneddon
<http://gsnedders.com/>
More information about the lxml-dev
mailing list