Re: [lxml] Test whether a (removed?) element is in an element tree
Hi,
Suppose I have an element tree:
xml = lxml.etree.XML(b"<a>bla <b>bla</b> bla <c>bla</c> bla <d/> bla</a>”)
and I get all of its elements:
elems = xml.xpath(".//*”) [
, , ] Next, I remove some element from that tree:
c = xml.find("c”) # = elems[1] c.getparent().remove(c)
How can I now test whether that element is still in the element tree?
c.getparent() # elems[1].getparent()
returns None, but is that a sure test? I can’t find documentation on how to identify elements that don’t belong to a given element tree.
IIRC: getparent() also returns None for a root element. As far as I remember each element always belongs to one unique tree, which gets created on-the-fly for an element that you remove from a tree. So I don't quite see the usecase, but you could check if c.getroottree() is the same tree object as your original/other tree (i.e. xml.getroottree()). Best regards Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart HRA 4356, HRA 104 440 Amtsgericht Mannheim HRA 40687 Amtsgericht Mainz Die LBBW verarbeitet gemaess Erfordernissen der DSGVO Ihre personenbezogenen Daten. Informationen finden Sie unter https://www.lbbw.de/datenschutz.
participants (2)
-
Holger.Joukl@LBBW.de
-
Stefan Behnel