{"record":{"id":"4f3b5c2c983a3ee7","repo":"antlr/antlr4","slug":"unknown-path-element-s","errorCode":null,"errorMessage":"Unknown path element %s","messagePattern":"Unknown path element (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/xpath/XPath.py","lineNumber":125,"sourceCode":"                else:\n                    el = next_el\n            # Check for bangs\n            if el.type == XPathLexer.BANG:\n                invert = True\n                next_el = next(tokens, None)\n                if not next_el:\n                    raise Exception('Missing element after %s' % el.getText())\n                else:\n                    el = next_el\n            # Add searched element\n            if el.type in [XPathLexer.TOKEN_REF, XPathLexer.RULE_REF, XPathLexer.WILDCARD, XPathLexer.STRING]:\n                element = self.getXPathElement(el, anywhere)\n                element.invert = invert\n                elements.append(element)\n            elif el.type==Token.EOF:\n                break\n            else:\n                raise Exception(\"Unknown path element %s\" % lexer.symbolicNames[el.type])\n        return elements\n\n    #\n    # Convert word like {@code#} or {@code ID} or {@code expr} to a path\n    # element. {@code anywhere} is {@code true} if {@code //} precedes the\n    # word.\n    #\n    def getXPathElement(self, wordToken:Token, anywhere:bool):\n        if wordToken.type==Token.EOF:\n            raise Exception(\"Missing path element at end of path\")\n\n        word = wordToken.text\n        if wordToken.type==XPathLexer.WILDCARD :\n            return XPathWildcardAnywhereElement() if anywhere else XPathWildcardElement()\n\n        elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]:\n            tsource = self.parser.getTokenStream().tokenSource\n","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/xpath/XPath.py#L107-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"Malformed paths like '/expr///*' or paths with repeated separators where an element was expected; also token streams from a mismatched XPathLexer version.","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"],"exampleFix":"# before\nnodes = XPath.findAll(tree, \"/expr///*\", parser)\n\n# after\nnodes = XPath.findAll(tree, \"/expr/*\", parser)","handlingStrategy":"try-catch","validationCode":"import re\nif re.search(r'//{2,}|/\\s*/(?!/)', xpath) or xpath.count('/') > 2 * len(re.findall(r'[A-Za-z_*\\\"]+', xpath)):\n    raise ValueError('suspect separator placement in XPath')","typeGuard":null,"tryCatchPattern":"try:\n    nodes = XPath.findAll(tree, xpath, parser)\nexcept Exception as e:\n    if str(e).startswith('Unknown path element'):\n        # simplify path to separator/element alternation\n        ...","preventionTips":["Keep paths in strict separator/element alternation","Avoid hand-concatenating separators"],"tags":["antlr","python","xpath","validation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}