Skip to content

Fix XPath comparisons between a node-set and a boolean, number or string - #383

Open
bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-xpath-node-set-compare
Open

bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-xpath-node-set-compare

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

Comparisons between a node-set and another value give wrong results in three cases:

query master this PR libxml2
/root/item[10 > @price] over price="5", price="50" 50 5 5
150 > /root/child over 200, 300 true false false
'150' > /root/child over 200, 300 true false false
/root/missing = false() false true true
false() < /root/child (non-empty) false true true
/root/child = '100.0' over 100 true false false

Cause:

  • When the node-set was the right operand, the code swapped the operands so the node-set came first but kept the operator, so 10 > @price was evaluated as @price > 10.
  • For a boolean, each node was converted to a boolean and compared on its own. With an empty node-set nothing was compared, so //missing = false() was false.
  • A string made of digits was converted to a number even for = / !=, so @n = '1.0' matched n="1".

https://www.w3.org/TR/xpath-10/#booleans

If one object to be compared is a node-set and the other is a number, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the number to be compared and on the result of converting the string-value of that node to a number using the number function is true. If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true. If one object to be compared is a node-set and the other is a boolean, then the comparison will be true if and only if the result of performing the comparison on the boolean and on the result of converting the node-set to a boolean using the boolean function is true.

Now the node value is always put on the node-set's side, a boolean is compared with boolean(node-set), and a string is compared as a string. The existing compare already turns strings into numbers for <, <=, >, >=, so '199' < /root/child still compares numbers.

The six new tests fail on master and pass here. bundle exec rake test passes (847 tests). I also ran ~2,300 XPath expressions against libxml2 through lxml: every result this PR changes now matches libxml2, and nothing else changed. The only comparison differences left are nodes like <v>1e2</v>, which libxml2 reads as 100 but XPath 1.0's number() reads as NaN.

When the node-set was on the right side, the operands were swapped
without flipping the operator, so 10 > @price was evaluated as
@price > 10. A boolean was compared with each node instead of with
boolean(node-set), so //missing = false() was false. A string that
looked like a number was compared as a number, so @n = '1.0' matched
n="1".
Copilot AI balanced review requested due to automatic review settings November 18, 2026 05:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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