Repository navigation
Count last() over the following and following-sibling axes - #188
Merged
Merged
Conversation
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.
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.
last()in a predicate on thefollowingorfollowing-siblingaxis 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, sofollowing-sibling::*[last()]never matched.Using
employee_examplefrom the tests (line numbers)://employee[@id=1]/following-sibling::*[last()]//employee[@id=1]/following-sibling::*[last()-1]//employee/name/following-sibling::*[last()]//employee[@id=1]/following::*[last()]//employee[@id=1]/following::*[last()-1]Root cause
lastFuncworks 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, solast()returns the wrong number.Fix
filterQuery.doalready builds apositionIteratorfor positional predicates. It now also records the query being filtered (input).lastFuncasks that query for its context size through a newgetContextSizehelper (contextSize() int, next togetNodePosition). If the query cannot count, the existing sibling walk is used, so the child axis behaves exactly as before.followingQueryrecords its context node. WithSiblingset, 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,positionIteratorandgetContextSizehunk 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_lastinxpath_axes_test.gocovers both axes, includinglast()-1,position()>last()-2,position()<last(),last()=8, and multiple context nodes.If I revert only the axis-specific part of
query.goand keep the shared hook, the test fails withexpected lines [16], got [10].Commands
go test ./...: okgo vet ./...: exit 0The 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.