Skip to content

Count last() over the following and following-sibling axes - #188

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
rudycelekli:fix/last-following-axis
Nov 7, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
rudycelekli:fix/last-following-axis

Conversation

@rudycelekli

Copy link
Copy Markdown
Contributor

last() in a predicate on the following or following-sibling axis returns the wrong context size. In XPath 1.0 §2.4, the context size is the number of nodes the step selects for the context node. Instead, the count included siblings before the context node, so following-sibling::*[last()] never matched.

Using employee_example from the tests (line numbers):

Expression Before Expected
//employee[@id=1]/following-sibling::*[last()] (none) 13
//employee[@id=1]/following-sibling::*[last()-1] 13 8
//employee/name/following-sibling::*[last()] (none) 6, 11, 16
//employee[@id=1]/following::*[last()] 10 16
//employee[@id=1]/following::*[last()-1] 9 15

Root cause

lastFunc works out the context size by walking the siblings of the candidate node and counting those that pass the step's node test. That only matches the context size on the child axis. On this axis the siblings of the candidate are not the nodes the step selects for the context node, so last() returns the wrong number.

Fix

  • filterQuery.do already builds a positionIterator for positional predicates. It now also records the query being filtered (input).
  • lastFunc asks that query for its context size through a new getContextSize helper (contextSize() int, next to getNodePosition). If the query cannot count, the existing sibling walk is used, so the child axis behaves exactly as before.
  • followingQuery records its context node. With Sibling set, it counts the following siblings that match its node test; otherwise it counts the matching nodes in the following siblings of the context node and of each of its ancestors, together with their descendants. The count is computed once per context node and cached.

The lastFunc, positionIterator and getContextSize hunk is identical in the sibling PRs for the other axes (attribute, following, preceding, descendant, ancestor, self and parent). Git merges identical hunks cleanly, so these PRs can be merged in any order.

Test

Test_following_last in xpath_axes_test.go covers both axes, including last()-1, position()>last()-2, position()<last(), last()=8, and multiple context nodes.

If I revert only the axis-specific part of query.go and keep the shared hook, the test fails with expected lines [16], got [10].

Commands

  • go test ./...: ok
  • go vet ./...: exit 0

The expected column follows from the spec text above. I also compared the same last() expressions on this axis against lxml (libxml2 2.9.13) with a differential harness on a separate document, and the fixed results agree.

last() in a predicate on the following or following-sibling axis counted
all siblings of the candidate node that the node test matched, so the
count included nodes before the context node. following-sibling::*[last()]
matched nothing, and following::*[last()] returned a node near the start
of the axis.

A predicate now passes the query it filters to last(), and an axis query
that can count the nodes it selects for its context node reports that
count. The following query counts the nodes on its axis.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 87.335% (+0.1%) from 87.211% — rudycelekli:fix/last-following-axis into antchfx:master

@zhengchun
zhengchun merged commit d2ed292 into antchfx:master Nov 7, 2026
3 checks passed
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.

3 participants