antlr/antlr4 · error · ArgumentException
Unknowth path element ${el}
Error message
Unknowth path element ${el} What it means
XPath.Split() accepts only the small XPath dialect understood by the ANTLR tree XPath support: '/', '//', '*', token references, rule references, and optional '!' inversion immediately after a separator. The default branch of the token switch is reached when the lexer produced a token that is not one of those expected top-level elements. This almost always means the path syntax is valid enough to lex but is not valid for this XPath subset.
Source
Thrown at runtime/CSharp/src/Tree/Xpath/XPath.cs:162
}
case XPathLexer.TokenRef:
case XPathLexer.RuleRef:
case XPathLexer.Wildcard:
{
elements.Add(GetXPathElement(el, false));
i++;
break;
}
case TokenConstants.EOF:
{
goto loop_break;
}
default:
{
throw new ArgumentException("Unknowth path element " + el);
}
}
}
loop_break: ;
return elements.ToArray();
}
private sealed class _XPathLexer_87 : XPathLexer
{
public _XPathLexer_87(ICharStream baseArg1)
: base(baseArg1)
{
}
public override void Recover(LexerNoViableAltException e)
{
throw e;
}View on GitHub (pinned to 7d5770395b)
Solutions
- Use only supported forms: '/rule', '//rule', '/TOKEN', '//TOKEN', '*', and '/!' or '//!' before the element for inversion.
- Remove a leading or misplaced '!' and place inversion directly after a separator.
- If quoting a token name, keep the leading separator, e.g. "/'TOKEN'" rather than "'TOKEN'".
- Inspect the generated XPathLexer token types if the path still fails, and remove unsupported XPath constructs.
Example fix
// before var p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, "!ID"); // after var p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, "/!ID");
Defensive patterns
Strategy: validation
Validate before calling
static bool IsSupportedXPathShape(string path) {
if (string.IsNullOrWhiteSpace(path)) return false;
return !path.StartsWith("!") && System.Text.RegularExpressions.Regex.IsMatch(
path,
"^(\/\/?!?[A-Za-z_\u00C0-\uFFFF][A-Za-z0-9_\u00C0-\uFFFF]*|\/\/?'?[^']*'?|\\*)*$");
} Try / catch
try { var p = new Antlr4.Runtime.Tree.Xpath.XPath(parser, xpath); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Unknowth path element")) { /* reject xpath */ } Prevention
- Keep XPath strings limited to '/', '//', names, '*', and optional '!' immediately after a separator.
- Do not use full XPath predicates, axes, or '..' with this class.
- Build paths from a whitelist of components rather than raw concatenation.
When it happens
Trigger: A path begins with '!' without a preceding '/' or '//' (for example "!ID"); a quoted string is used as the first element (for example "'ID'" instead of "/ID" or "/'ID'"); or stray characters/tokens such as a top-level Bang appear after a word. Full XPath features such as predicates, '..', or axes are not supported and can also lead here or to the invalid-character error.
Common situations: Developers mistake Antlr4.Runtime.Tree.Xpath.XPath for a full XPath engine; hand-built path strings contain a trailing separator or misplaced '!'; paths are assembled by string concatenation; or documentation examples from a different runtime version are copied.
Related errors
- Missing path element at end of path
- Could not read path: ${path}
- Invalid tokens or characters at index ${pos} in path '${path
- ${word} at index ${startIndex} isn't a valid token name
- ${word} at index ${startIndex} isn't a valid rule name
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/0c9ee456d09a0dd3.
Report an issue: GitHub.