Skip to content
Advertisement

I am not able to resolve ‘lxml.etree.XPathEvalError: Invalid expression’ error on a legal XPATH expression

I am trying to parse an xpath but it is giving Invalid expression error.

The code that should work:

x = tree.xpath("//description/caution[1]/preceding-sibling::*/name()!='warning'")
print(x)

Expected result is a boolean value but it is showing error:

Traceback (most recent call last):
  File "poc_xpath2.0_v1.py", line 9, in <module>
    x = tree.xpath("//description/caution[1]/preceding-sibling::*/name()!='warning'")
  File "srclxmletree.pyx", line 2276, in lxml.etree._ElementTree.xpath
  File "srclxmlxpath.pxi", line 359, in lxml.etree.XPathDocumentEvaluator.__call__
  File "srclxmlxpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression

Advertisement

Answer

The exception is because name() isn’t a valid node type. Your XPath would only be valid as XPath 2.0 or greater. lxml only supports XPath 1.0.

You would need to move the name() != 'warning' into a predicate.

Also, if you want a True/False result, wrap the xpath in boolean()

tree.xpath("boolean(//description/caution[1]/preceding-sibling::*[name()!='warning'])")

Full example…

from lxml import etree

xml = """
<doc>
    <description>
        <warning></warning>
        <caution></caution>
    </description>
</doc>"""

tree = etree.fromstring(xml)

x = tree.xpath("boolean(//description/caution[1]/preceding-sibling::*[name()!='warning'])")
print(x)

This would print False.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement