XPath SyntaxError: invalid predicate with text()

Question:

What am I doing wrong?

When using what I believe to be a valid XPath expression in Python, I get a "SyntaxError: invalid predicate" result. My code is:

import xml.etree.ElementTree as ET
data = '''<root>
            <child>
                <p>aaa</p>
                <p>bbb</p>
            </child>
        </root>'''

root = ET.fromstring(data)

p_element = root.findall(".//p[text()='aaa']")

print(p_element[0].text)

Running this code produces an invalid predicate error, but I believe it is correct. I’m using Python version 3.10.0, and lxml version 4.9.2, and running this on Windows.

It seems to have something to do with the "text()" part of the XPath; with that removed, it does not error.

Asked By: anon

||

Answers:

.//p[text()='aaa'] is a perfectly valid XPath expression.

The problem is that ElementTree’s implementation of XPath is incomplete.

For your XML, testing the string value, .//p[.='aaa'], would be a work-around if you’re stuck with ElementTree. Better would be to only use an XPath library such as lxml that better follows the standard.

See also

Answered By: kjhughes
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.