Skip to content

Keep position() on the step when a predicate path comes first - #158

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
SashaMIT:fix/position-predicate-step
Oct 14, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
SashaMIT:fix/position-predicate-step

Conversation

@SashaMIT

@SashaMIT SashaMIT commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes antchfx/htmlquery#40.

//div[@class='test' and position()=1] returned every div with that class. //div[position()=1 and @class='test'] returned only the first. position() is the position of the node in the step being filtered. Compiling @class, or a child path, earlier in the same predicate replaced that step, so position() counted the wrong nodes and stayed at 1.

TestPositionKeepsStepWhenPredicateHasAPath expects line 4 for position()=1 and lines 5 and 8 for position()=2. Without the pin, position()=1 returns lines 4, 5, and 8.

A caller that asks for the first match and then uses every returned node acts on the rest of the set. This is the wrong node set.

position() and last() record the step being filtered. Compiling
@Class or a child path earlier in the same predicate replaced that
step, so position() counted the wrong nodes and stayed at 1.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 84.785% (+0.08%) from 84.707% — SashaMIT:fix/position-predicate-step into antchfx:master

@zhengchun
zhengchun merged commit d11796a into antchfx:master Oct 14, 2026
3 checks passed
@SashaMIT

Copy link
Copy Markdown
Contributor Author

Thanks for merging it. position() stays on the step when a path comes first in the predicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Xpath position function not working properly

3 participants