wmjordan/PDFPatcher · error · FormatException
“[]”筛选表达式前缺少节点轴及节点名称标识。
Error message
“[]”筛选表达式前缺少节点轴及节点名称标识。
What it means
PathCompiler.ExtractAxis throws FormatException when the current path character is '[' or ']' (the predicate delimiters) at a position where an axis separator or node name was expected. In the PDFPath mini-language, a predicate '[...]' must follow a node name or axis; encountering a bracket where an axis is required means the path string is malformed.
Source
Thrown at App/Model/PdfPath/PathCompiler.cs:48
string n;
var ctx = new Context();
while (i < l) {
ctx.Axis = ExtractAxis(path, l, ctx.CanBeRoot, ref i);
if (ctx.Axis == PathAxisType.Root) {
ctx.CanBeRoot = false;
r.Enqueue(new PathExpression(ctx.Axis));
continue;
}
n = ExtractName(path, l, ref i);
r.Enqueue(new PathExpression(ctx.Axis, n));
}
return r.ToArray();
}
private static PathAxisType ExtractAxis(string path, int length, bool canBeRoot, ref int index) {
char c = path[index];
if (__PredicateChars.Contains(c)) {
throw new FormatException("“[]”筛选表达式前缺少节点轴及节点名称标识。");
}
if (c == PathSeparator) {
if (MatchNextChar(path, length, index, PathSeparator)) {
++index;
return PathAxisType.Descendants;
}
else if (canBeRoot) {
return PathAxisType.Root;
}
else {
return PathAxisType.Children;
}
}
else if (c == SelfChar) {
if (MatchNextChar(path, length, index, SelfChar)) {
++index;
return PathAxisType.Parent;
View on GitHub (pinned to 4782bbd9ad)
Solutions
- Ensure every '[...]' predicate is preceded by a node name or axis (e.g. 'Page[1]' not '[1]').
- Validate the path string with a regex or a dry-run compile before using it for selection.
- If building paths dynamically, always concatenate the element name before appending the predicate.
Example fix
// before
var expr = PathCompiler.Compile("[1]");
// after
var expr = PathCompiler.Compile("Page[1]"); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(path) || path[0] == '[' || path[0] == ']')
throw new ArgumentException("Path must start with a node name or axis."); Type guard
static bool IsValidPathStart(string p) => !string.IsNullOrEmpty(p) && p[0] != '[' && p[0] != ']';
Try / catch
try { var e = PathCompiler.Compile(path); }
catch (FormatException ex) { /* malformed path */ } Prevention
- Always prefix predicates with a node name or axis.
- Dry-run compile paths built from user/template input.
When it happens
Trigger: Calling PathCompiler.Compile with a path that begins with or otherwise places a '[' before any node name or axis, e.g. "[1]" or "/[1]". The loop in Compile calls ExtractAxis first, and a leading bracket trips __PredicateChars.Contains(c).
Common situations: Programmatically building a path string and forgetting to prepend the node name before a filter; user-entered PDFPath expression with a missing element name; a template substitution that left an empty name before a predicate.
Related errors
AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13).
Data as JSON: /api/errors/c5dbd8ab2d0b5c8d.
Report an issue: GitHub.