Repository navigation
Support positional predicates on the attribute axis - #171
Merged
zhengchun merged 1 commit intoOct 28, 2026
Merged
Conversation
On an element with three attributes, `/root/item/@*[1]` selected all three, and `@*[2]`, `@*[3]` and `[position()=2]` selected none. XPath 1.0 section 2.4 takes the position of a node in a predicate with respect to its axis, so give attributeQuery a position counted over the attributes of each element that pass its node test, as childQuery does. The `//attribute::*[1]` case in Test_attribute is enabled as a count, since employee_example adds the attributes of an element in map order.
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.
On
<item a="1" b="2" c="3"/>,/root/item/@*[1]selects all three attributes, while@*[2],@*[3],attribute::*[2]and@*[position()>1]select nothing.(/root/item/@*)[2]already works.XPath 1.0 section 2.4 evaluates a predicate with "the proximity position of the node in the node-set with respect to the axis as the context position".
getNodePositionreturns 1 for a query without aposition()method (query.go), andattributeQueryhas none, unlike the child, ancestor, descendant, following and preceding queries. So[1]matches every attribute and[2]none.attributeQuerynow counts the attributes that pass its node test, from 1 again for each element, aschildQuerydoes.Across
itemelements with the attributesa b c,a bandc(values 1 to 6),//@*[1]now selects1,4and6instead of all six,/root/item/@*[2]selects2and5, and/root/item[@*[2]]/@aselects1and4.last()is still wrong on this axis:@*[last()]now selects the first attribute of each element, where it used to select all of them. That is a separate change.Test_attribute_positionadds the attributes in a fixed order, sinceemployee_exampleadds those of an element in map order. The commented-out//attribute::*[1]case inTest_attributeis enabled as a count (7), since its tag list depends on that order. Both tests fail on master.