I'm trying to compile an XPath expression using etree.XPath in Python, but I'm encountering a syntax error. Here's the code snippet:
XPATH = '//bridge-domain/(bridge-domain-group-name|bridge-domain-name|bridge-acs/bridge-ac/interface-name|bridge-acs/bridge-ac/attachment-circuit/state|bridge-access-pws/bridge-access-pw/neighbor|bridge-access-pws/bridge-access-pw/pseudowire/state)'
try:
xpath_obj = etree.XPath(XPATH)
print("XPath expression compiled successfully.")
except etree.XPathSyntaxError as e:
print(f"XPath syntax error: {e}")
Upon running this code, I receive the following error message:
XPath syntax error: Invalid expression
The XPath expression causing the error is:
XPATH = '//bridge-domain/(bridge-domain-group-name|bridge-domain-name)'
Could someone please explain what might be causing this error and suggest how to correct the XPath expression so that it compiles correctly? Any guidance or examples would be greatly appreciated.
You are relegated to XPath 1.0 expressions with lxml.
You cannot use the union operator within a step selecting multiple items in parens. You will need to use a more verbose XPath that repeats the //bridge-domain
for each of the unioned expressions:
XPATH = """//bridge-domain/bridge-domain-group-name
| //bridge-domain/bridge-domain-name
| //bridge-domain/bridge-acs/bridge-ac/interface-name
| //bridge-domain/bridge-acs/bridge-ac/attachment-circuit/state
| //bridge-domain/bridge-access-pws/bridge-access-pw/neighbor
| //bridge-domain/bridge-access-pws/bridge-access-pw/pseudowire/state"""