I have an XML file, foo.xml
:
<foo>
<bar>
<baz phrase="hello"/>
</bar>
<quux phrase="goodbye"/>
</foo>
I'm parsing it with this Python code:
import lxml.etree as ET
# or if you don't have lxml: import xml.etree.ElementTree as ET
doc = ET.parse('foo.xml').getroot()
for elem in doc.findall('*[@phrase]'):
print(elem)
That gives me:
<Element 'quux' at 0x7fa1419a1d18>
Now I want to find all elements with a phrase
attribute, so I tried './/[@phrase]'
but then findall()
fails:
SyntaxError: invalid descendant
I don't understand what's wrong. The same error message appears if I use the built-in xml.etree.ElementTree
instead of lxml
.
Note that './/'
works, but returns bar, baz, quux
and I don't want bar
because it doesn't have a phrase
attribute.
*
in.//*[@phrase]
– Shirley