I want to get a list of all trololo tags with attr attribute (but not xxx or any other) using Python from following XML:
<data>
<test>
<trololo attr="1">
</trololo>
</test>
<test>
<trololo>
</trololo>
</test>
<test>
<trololo attr="X">
</trololo>
</test>
<test>
<xxx attr="Y">
</xxx>
</test>
</data>
I've tried using //*[@attr], but result includes xxx tag as well. All other variations I tried are failing so far.
The actual Python code I'm using:
import xml.etree.ElementTree as ET
from pprint import pprint
tree = ET.parse('test.xml')
nodes = tree.findall('//*trololo[@attr]')
pprint(nodes)
Output:
[]
UPDATE:
I've found out this was a namespace problem, which makes this question a duplicate. The problem was I had a root node looking like this:
<data xmlns="http://example.com">
<trololo>nodes. They might be 100 levels below the root.[<Element 'trololo' at 0x7fab55c90ef8>, <Element 'trololo' at 0x7fab55c903b8>]nodes = tree.findall('//trololo[@attr]')i.e without*?