Python ElementTree "Invalid descendant" error
Asked Answered
A

1

6

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.

Academician answered 14/11, 2016 at 5:21 Comment(2)
you need * in .//*[@phrase]Shirley
@Shirley that should be an answerAdmiralty
S
10

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

Shirley answered 14/11, 2016 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.