0

I am trying to find the deepest-nested children element of each element in an xml tree. I am currently using the lxml library, specifically etree.

I am using the following xml code as an example:

<item>
  <a1>value1</a1>
  <a2>value2</a2>
  <a3>value3</a3>
  <a4>
    <a11>value222</a11>
    <a22>value22</a22>
  </a4>
</item>

I would like the output to show that the a1, a2 and a3 elements have no child elements and that a11 and a22 are the deepested nested children of the a4 element.

1 Answers1

0

XPath

Basically you'd like to see all elements whose list of child elements is empty. An empty list in XPath can be converted to false. So the XPath expression you want to resolve could be (not tested):

//*             -> deliver any element
//*[*]          -> deliver any element that has children
//*[*=false]    -> deliver any element that has no children 

Also see How to use XPath in Python?

Custom

You can parse the document into a DOM tree, then traverse that tree and list all nodes where getChildNode() returns a list that does not contain any further element.

Queeg
  • 7,748
  • 1
  • 16
  • 42