I have a XML file which has structure like this:
<element1>
<element2>
...
<element10 name="a">
...
I am trying to parse this file in Python 2.7, using The ElementTree XML API. But the data I am looking for are deeply hidden in the structure.
Is there any way I can set specific great-great-...-grand child element (for example the element10) as the root element without iterating the whole structure?
Try this :
'(//*[starts-with(name(), "element")])[last()]'
$ cat file
<element1>
<element2></element2>
<element3></element3>
<element4></element4>
<element5></element5>
<element6></element6>
<element10 name="a">x</element10>
</element1>
(works with xmllint too)
$ saxon-lint.pl --xpath '(//*[starts-with(name(), "element")])[last()]' file
<element10 name="a">x</element10>
I have finally solved it thanks to this great article.
tree.iter(tag = 'element10')
This will find the needed element in the structure and then you can iterate his child elements, even if there are more 'element10' named elements in the tree.
for element in tree.iter(tag = 'element10'):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With