diff --git a/lib/rexml/xpath_parser.rb b/lib/rexml/xpath_parser.rb index 3da7635b..92ec0b8d 100644 --- a/lib/rexml/xpath_parser.rb +++ b/lib/rexml/xpath_parser.rb @@ -1154,38 +1154,28 @@ def equality_relational_compare(set1, op, set2) compare(node_string1, op, node_string2) end elsif set1.kind_of? Array or set2.kind_of? Array - # If one is nodeset and other is number, compare number to each item - # in nodeset s.t. number op number(string(item)) - # If one is nodeset and other is string, compare string to each item - # in nodeset s.t. string op string(item) - # If one is nodeset and other is boolean, compare boolean to each item - # in nodeset s.t. boolean op boolean(item) - if set1.kind_of? Array - a = set1 - b = set2 - else - a = set2 - b = set1 - end - - case b - when true, false - a.any? do |node| - compare(@functions.boolean(node), op, b) - end - when Numeric - a.any? do |node| - compare(@functions.number(node), op, b) - end - when /\A\d+(\.\d+)?\z/ - b = @functions.number(b) - a.any? do |node| - compare(@functions.number(node), op, b) + if [set1, set2].any? {|value| value == true or value == false} + # 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. + compare(@functions.boolean(set1), op, @functions.boolean(set2)) + elsif set1.kind_of? Array + # If one object to be compared is a node-set and the other is + # a number (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 number to be compared + # and on the result of converting the string-value of that + # node to a number using the number function (on the + # string-value of the node and the other string) is true. + set1.any? do |node| + compare(node_set_item_value(node, set2), op, set2) end else - b = @functions.string(b) - a.any? do |node| - compare(@functions.string(node), op, b) + # Keep the operand order: "1 < @n" is not "@n < 1". + set2.any? do |node| + compare(set1, op, node_set_item_value(node, set1)) end end else @@ -1200,6 +1190,14 @@ def equality_relational_compare(set1, op, set2) end end + def node_set_item_value(node, other) + if other.kind_of? Numeric + @functions.number(node) + else + @functions.string(node) + end + end + def value_type(value) case value when true, false diff --git a/test/xpath/test_compare.rb b/test/xpath/test_compare.rb index 11d11e55..095c0c8e 100644 --- a/test/xpath/test_compare.rb +++ b/test/xpath/test_compare.rb @@ -78,6 +78,33 @@ def test_string_false assert_equal([false], match(xml, "/root/child='nonexistent'")) end + + def test_boolean_empty + xml = <<-XML + + + + XML + assert_equal([[true], [false]], + [ + match(xml, "/root/child=false()"), + match(xml, "false()!=/root/child"), + ]) + end + + def test_string_number_like + xml = <<-XML + + + 100 + + XML + assert_equal([[false], [true]], + [ + match(xml, "/root/child='100.0'"), + match(xml, "'100'=/root/child"), + ]) + end end class TestBoolean < self @@ -192,6 +219,62 @@ def test_string_false assert_equal([false], match(xml, "/root/child>'200'")) end + + def test_boolean_left + xml = <<-XML + + + + + XML + assert_equal([[false], [true]], + [ + match(xml, "false()>/root/child"), + match(xml, "true()>/root/nonexistent"), + ]) + end + + def test_number_left + xml = <<-XML + + + 200 + 300 + + XML + assert_equal([[false], [true]], + [ + match(xml, "150>/root/child"), + match(xml, "250>/root/child"), + ]) + end + + def test_string_left + xml = <<-XML + + + 200 + 300 + + XML + assert_equal([[false], [true]], + [ + match(xml, "'150'>/root/child"), + match(xml, "'250'>/root/child"), + ]) + end + + def test_predicate_number_left + xml = <<-XML + + + + + + XML + assert_equal(["5"], + match(xml, "/root/item[10>@price]").collect {|item| item.attributes["price"]}) + end end class TestBoolean < self