Repository navigation
fix(xpath): pre-compile expressions and sort union (|) results in DOM document order - #138
Open
Divyanshu Tiwari (divyanshu-tiwari) wants to merge 7 commits into
Open
Divyanshu Tiwari (divyanshu-tiwari) wants to merge 7 commits into
Divyanshu Tiwari (divyanshu-tiwari) wants to merge 7 commits into
Conversation
Divyanshu Tiwari (divyanshu-tiwari)
requested a review
from a team
as a code owner
November 6, 2026 07:42
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
XPath union ordering remains incorrect because detached attributes are appended after DOM nodes and selected document nodes can appear after their descendant container.
2 open issues
What changed in this PR
This PR addresses branch-dependent XPath union ordering so container node_index values and field arrays follow DOM document order.
Changes:
- Routes container and field queries through a shared
findNodeshelper. - Adds pre-order DOM traversal to sort multi-node union results.
- Documents document-order behavior for matches and container indices.
| File | Description |
|---|---|
internal/pkg/pipeline/task/xpath/xpath.go |
Adds union-result sorting for container and field queries. |
internal/pkg/pipeline/task/xpath/README.md |
Documents match ordering and node_index semantics. |
Review effort: Balanced
Ankur Gupta (ankur-pattern)
approved these changes
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
github.com/antchfx/xpathevaluates union (|) expressions branch-by-branch from left to right and concatenates the results rather than returning the node-set in global DOM document order as required by the XPath 1.0 specification.xpathtask assignsnode_index(and populates multi-value field arrays) directly from the returned slice order, permuting the branches of a|expression previously alterednode_indexvalues and output ordering.containerandfieldsXPath expressions once duringxpath.Init()ininternal/pkg/pipeline/task/xpath/xpath.goso invalid XPath syntax fails fast at pipeline initialization instead of panicking at runtime, and worker goroutines avoid per-query global LRU mutex contention.internal/pkg/pipeline/task/xpath/query.go(compiledXPath,hasUnionOperator,matchIndex,sortMatchesInDocumentOrder) to detect unquoted|operators and order union matches (elements, text, attributes via their owning DOM element and tag attribute order, and out-of-container/document-root matches) via a pre-order DOM traversal with zero overhead for non-union XPath expressions.internal/pkg/pipeline/task/xpath/README.mdto document that matched container and field nodes are returned in DOM document order.Benchmarks
Evaluated on
darwin/arm64(Apple M5 Pro) using multi-branch union (|) XPath expressions against two complete live HTML documents:htmlquery.Find)compiledXPath.find)sortMatchesInDocumentOrder)4.28 ms/op(4,282,326 ns/op)4.59 ms/op(4,587,528 ns/op)81.4 µs/op(81,422 ns/op, ~1.9% of query time)9,655,432 B/op(336,883 allocs/op)9,656,835 B/op(336,889 allocs/op)888 B/op(5 allocs/op)5.39 ms/op(5,389,265 ns/op)5.39 ms/op(5,394,093 ns/op)69.5 µs/op(69,516 ns/op, ~1.3% of query time)10,690,434 B/op(387,751 allocs/op)10,692,442 B/op(387,757 allocs/op)1,496 B/op(5 allocs/op)