7

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.

2
  • 3
    you need * in .//*[@phrase] Commented Nov 14, 2016 at 5:24
  • @furas that should be an answer Commented Nov 14, 2016 at 5:35

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.