wmjordan/PDFPatcher · error · FormatException

“[]”筛选表达式前缺少节点名称。

Error message

“[]”筛选表达式前缺少节点名称。

What it means

PathCompiler.ExtractName throws FormatException when the character at the current index is '[' or ']' — i.e. when the parser expects a node name but finds a predicate delimiter. This differs from error [4]: here the axis was already consumed and the parser moved on to read the name, but the name position is occupied by a bracket, so the name is missing.

Source

Thrown at App/Model/PdfPath/PathCompiler.cs:80

			}
			else if (c == SelfChar) {
				if (MatchNextChar(path, length, index, SelfChar)) {
					++index;
					return PathAxisType.Parent;
				}
				else {
					return PathAxisType.None;
				}
			}
			else {
				return PathAxisType.Children;
			}
		}

		private static string ExtractName(string path, int length, ref int index) {
			char c = path[index];
			if (__PredicateChars.Contains(c)) {
				throw new FormatException("“[]”筛选表达式前缺少节点名称。");
			}
			if (c == UniversalName) {
				return null;
			}
			var n = new List<char>();
			while (Char.IsLetter(c) || n.Count > 0 && Char.IsLetterOrDigit(c)) {
				n.Add(c);
				++index;
				if (index < length) {
					c = path[index];
				}
				else {
					break;
				}
			}
			return n.Count > 0 ? new String(n.ToArray()) : null;
		}

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Ensure every axis token ('/', '//', '.', '..') is followed by a node name or '*' before any predicate.
  2. Validate the compiled path expression by testing it against a sample document before deployment.
  3. When constructing paths, insert the node name ('*' for universal) between the axis and any '[' filter.

Example fix

// before
var expr = PathCompiler.Compile("// [1]");

// after
var expr = PathCompiler.Compile("//*[1]");
Defensive patterns

Strategy: validation

Validate before calling

if (path.Contains("/[") || path.Contains("//[") || path.Contains(".["))
    throw new ArgumentException("Axis must be followed by a node name before a predicate.");

Type guard

static bool PathHasNameBeforePredicate(string p) =>
    !System.Text.RegularExpressions.Regex.IsMatch(p, @"(/|//|\.)\[");

Try / catch

try { var e = PathCompiler.Compile(path); }
catch (FormatException ex) { /* missing node name */ }

Prevention

When it happens

Trigger: Calling PathCompiler.Compile with a path where an axis separator ('/', '//', '.') is immediately followed by a predicate bracket, e.g. "/Page//[1]" or "..[1]" — the axis parsed but ExtractName sees '['. Also occurs for "/Page/[1]" sequences where a name was expected but a bracket appeared.

Common situations: Dynamically concatenating path segments and emitting an axis without the following node name; malformed user input; a double-slash descendants axis not followed by a name before a filter.

Related errors


AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13). Data as JSON: /api/errors/4b56a4c5a71397e0. Report an issue: GitHub.