Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ElementTree "Invalid descendant" error

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.

like image 376
John Zwinck Avatar asked Nov 14 '16 05:11

John Zwinck


1 Answers

You need * as tag name in ".//*[@phrase]"

like image 53
furas Avatar answered Nov 09 '22 00:11

furas