Skip to content

Support positional predicates on the attribute axis - #171

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
spicysharkco:fix/attribute-axis-position
Oct 28, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
spicysharkco:fix/attribute-axis-position

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

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".

getNodePosition returns 1 for a query without a position() method (query.go), and attributeQuery has none, unlike the child, ancestor, descendant, following and preceding queries. So [1] matches every attribute and [2] none. attributeQuery now counts the attributes that pass its node test, from 1 again for each element, as childQuery does.

                             before    after
/root/item/@*[1]             1, 2, 3   1
/root/item/@*[2]             (empty)   2
/root/item/@*[3]             (empty)   3
/root/item/attribute::*[2]   (empty)   2
/root/item/@*[position()=2]  (empty)   2
/root/item/@*[position()>1]  (empty)   2, 3

Across item elements with the attributes a b c, a b and c (values 1 to 6), //@*[1] now selects 1, 4 and 6 instead of all six, /root/item/@*[2] selects 2 and 5, and /root/item[@*[2]]/@a selects 1 and 4.

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_position adds the attributes in a fixed order, since employee_example adds those of an element in map order. The commented-out //attribute::*[1] case in Test_attribute is enabled as a count (7), since its tag list depends on that order. Both tests fail on master.

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.
@zhengchun
zhengchun merged commit 6a83bdc into antchfx:master Oct 28, 2026
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.

2 participants