antlr/antlr4 · error · Exception
Unknown path element %s
Error message
Unknown path element %s
What it means
Raised by XPath element parsing in the Python3 runtime when a token appears that is none of TOKEN_REF, RULE_REF, WILDCARD, STRING, or EOF — i.e. lexically valid but not usable at that position in a path. Typically stray punctuation that the lexer accepted but the parser loop does not expect.
Source
Thrown at runtime/Python3/src/antlr4/xpath/XPath.py:125
else:
el = next_el
# Check for bangs
if el.type == XPathLexer.BANG:
invert = True
next_el = next(tokens, None)
if not next_el:
raise Exception('Missing element after %s' % el.getText())
else:
el = next_el
# Add searched element
if el.type in [XPathLexer.TOKEN_REF, XPathLexer.RULE_REF, XPathLexer.WILDCARD, XPathLexer.STRING]:
element = self.getXPathElement(el, anywhere)
element.invert = invert
elements.append(element)
elif el.type==Token.EOF:
break
else:
raise Exception("Unknown path element %s" % lexer.symbolicNames[el.type])
return elements
#
# Convert word like {@code#} or {@code ID} or {@code expr} to a path
# element. {@code anywhere} is {@code true} if {@code //} precedes the
# word.
#
def getXPathElement(self, wordToken:Token, anywhere:bool):
if wordToken.type==Token.EOF:
raise Exception("Missing path element at end of path")
word = wordToken.text
if wordToken.type==XPathLexer.WILDCARD :
return XPathWildcardAnywhereElement() if anywhere else XPathWildcardElement()
elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]:
tsource = self.parser.getTokenStream().tokenSource
View on GitHub (pinned to 7d5770395b)
Solutions
- Simplify the path to strictly alternating separator/element form
- Eliminate duplicated or misplaced separators and punctuation
- Print the token types of the path (debug the token stream) to find which token fell through
Example fix
# before nodes = XPath.findAll(tree, "/expr///*", parser) # after nodes = XPath.findAll(tree, "/expr/*", parser)
Defensive patterns
Strategy: try-catch
Validate before calling
import re
if re.search(r'//{2,}|/\s*/(?!/)', xpath) or xpath.count('/') > 2 * len(re.findall(r'[A-Za-z_*\"]+', xpath)):
raise ValueError('suspect separator placement in XPath') Try / catch
try:
nodes = XPath.findAll(tree, xpath, parser)
except Exception as e:
if str(e).startswith('Unknown path element'):
# simplify path to separator/element alternation
... Prevention
- Keep paths in strict separator/element alternation
- Avoid hand-concatenating separators
When it happens
Trigger: A path token stream containing an unexpected token type after separator/bang handling, e.g. an extra '/' in the middle producing a bare separator token where an element is required.
Common situations: Malformed paths like '/expr///*' or paths with repeated separators where an element was expected; also token streams from a mismatched XPathLexer version.
Related errors
- Missing element after %s
- Missing path element at end of path
- %s at index %d isn't a valid token name
- %s at index %d isn't a valid rule name
- The current recognizer does not provide a list of token name
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/4f3b5c2c983a3ee7.
Report an issue: GitHub.