antchfx/xpath is the Go XPath engine that sits under htmlquery and xmlquery, which means a large share of Go scraping pipelines evaluate their selectors through it. The last week brought 9 commits touching 18 files with 401 insertions. Almost all of them are semantic correctness fixes: positional predicates, union ordering, duplicate nodes on the parent axis, and stricter function argument handling. If your extraction logic depends on subtle XPath semantics, this window matters.
Positional predicates now count per context node ¶
The headline fix is the handling of chained predicates such as //a[@x][1]. Before, the position was evaluated against the wrong input. The correct XPath semantics say that in a[@x][1], position [1] counts the nodes left after the [@x] filter, per context node. The fix in 9954b1f rewrites the builder in build.go so the positional step is pushed below the earlier predicate filter. The commit walks down the chain of filterQuery inputs and reattaches the position there.
One subtlety is preserved in the code comment: last() is counted over the whole input by lastFuncQuery, so queries containing last() are deliberately left alone. That is a reasonable compromise given how the function is implemented.
The new test file xpath_predicate_test.go covers the edge cases well:
//a[@x][1]now returns the first matchingaelement under each parent, not the first matching element in the document(//a[@x])[2]still resolves against the whole node-set when the grouping parentheses are explicit/descendant::a[@x][1]behaves as a single context node case and returns one element
Anyone who previously worked around this bug by rewriting selectors with explicit grouping parentheses should retest. The workaround is no longer needed, and in some cases the old workaround now returns different nodes.
Unions return in document order and parent axes lose duplicates ¶
Two related fixes clean up result ordering and deduplication. f7c11ec makes union expressions return their results in document order. The XPath specification requires this, and downstream consumers that assume ordered output, for example streaming parsers feeding row extraction, no longer need a sort after the query.
c87a8e7 removes duplicate nodes from parent axis results. A node can reach the parent axis through multiple paths in a compound expression, and the engine was emitting the same node twice. query.go took three commits this window, which reflects how central the query evaluation code is to all of these fixes.
Together these two changes affect count and dedup logic in any pipeline that does count(a | b) style expressions or walks parent::*. Results that were inflated are now exact.
Function behavior gets stricter and closer to spec ¶
The function layer in func.go saw three changes:
- be7dd7a rejects invalid argument counts at evaluation time instead of silently returning something. This is a behavior change: expressions that “worked” by accident now fail loudly. That is the right tradeoff, but it can surface latent bugs in templates or configs that were feeding wrong arity all along.
- c6cc0aa lets
ends-withaccept node-set suffixes, matching the string conversion rules of the spec. - 696d996 returns an empty name for text and comment nodes instead of something misleading. Code that branches on
name()to distinguish element nodes from text and comments now behaves as expected.
The arity tests moved out of the single function_arity_test.go per 7eb6b3e and into the per feature test files. This is mostly internal refactoring; the coverage stays.
The namespace axis gets an honest error ¶
228b7dc reports the namespace axis as unsupported at compile time. Previously an expression using namespace::* either returned wrong results or failed in a confusing way deeper in evaluation. Failing at compile time with a clear message is strictly better for operators debugging broken selectors. Related, f7e25ee fixes attribute node stepping so selecting attributes from an attribute node yields nothing, which matches the spec and prevents odd results in paths like @x/@y.
What to watch ¶
- If you use chained positional predicates without grouping parentheses, run your extraction test suites against the updated engine. Several cases change the returned node set.
- Arity checking is now strict. Watch for evaluation errors in expressions that previously returned empty or garbage values on wrong argument counts.
- The
last()interaction with predicate rewritring is documented as an intentional exception in the builder. If you combinelast()with chained predicates, verify your expected results before upgrading, and file an issue with a minimal document if they differ.