From af4b355ae31fa2e762e26959d151543989ae60c3 Mon Sep 17 00:00:00 2001 From: Akshay Kumar Sharma <25038017+akzarma@users.noreply.github.com> Date: Wed, 2 Dec 2026 11:57:35 +0530 Subject: [PATCH] Fix XPath matching for the predefined XML namespace and empty strings --- lib/rexml/element.rb | 2 +- lib/rexml/namespace.rb | 7 +++++++ lib/rexml/parsers/baseparser.rb | 6 +++--- lib/rexml/xpath_parser.rb | 16 +++++----------- test/xpath/test_attribute.rb | 12 ++++++++++++ test/xpath/test_predicate.rb | 6 ++++++ 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/rexml/element.rb b/lib/rexml/element.rb index 8b6bdd1f..e8bc4d03 100644 --- a/lib/rexml/element.rb +++ b/lib/rexml/element.rb @@ -1527,7 +1527,7 @@ def _namespace_internal(namespaces = _calculate_namespaces) # :nodoc: # Lookup namespace for the given prefix in the context of the element def _namespace_lookup_internal(prefix, namespaces = _calculate_namespaces) # :nodoc: prefix = (prefix == '') ? 'xmlns' : prefix.delete_prefix("xmlns:") - ns = namespaces[prefix] + ns = Namespace.resolve_prefix(prefix, namespaces) ns = +'' if ns.nil? and prefix == 'xmlns' ns end diff --git a/lib/rexml/namespace.rb b/lib/rexml/namespace.rb index 232b7ca4..3544329e 100644 --- a/lib/rexml/namespace.rb +++ b/lib/rexml/namespace.rb @@ -13,6 +13,13 @@ module Namespace NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/ NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u + XML_URI = "http://www.w3.org/XML/1998/namespace" + + def self.resolve_prefix(prefix, namespaces) # :nodoc: + # XML Namespaces reserves this binding even without an xmlns declaration. + prefix == "xml" ? XML_URI : namespaces[prefix] + end + # Sets the name and the expanded name def name=( name ) @expanded_name = name diff --git a/lib/rexml/parsers/baseparser.rb b/lib/rexml/parsers/baseparser.rb index 8f20afb3..8ecb3a8c 100644 --- a/lib/rexml/parsers/baseparser.rb +++ b/lib/rexml/parsers/baseparser.rb @@ -5,6 +5,7 @@ require_relative '../source' require_relative '../text' require_relative '../xmltokens' +require_relative '../namespace' require 'set' require "strscan" @@ -162,7 +163,6 @@ module Private DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/ end DEFAULT_ENTITIES_PATTERNS.freeze - XML_PREFIXED_NAMESPACE = "http://www.w3.org/XML/1998/namespace" EXTERNAL_ID_PUBLIC_PATTERN = /\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um EXTERNAL_ID_SYSTEM_PATTERN = /\s+#{SYSTEMLITERAL}/um PUBLIC_ID_PATTERN = /\s+#{PUBIDLITERAL}/um @@ -201,7 +201,7 @@ def reset @tags = [] @stack = [] @entities = [] - @namespaces = {"xml" => Private::XML_PREFIXED_NAMESPACE} + @namespaces = {"xml" => Namespace::XML_URI} @namespaces_restore_stack = [] end @@ -870,7 +870,7 @@ def parse_attributes(prefixes) @source.skip_spaces if prefix == "xmlns" if local_part == "xml" - if value != Private::XML_PREFIXED_NAMESPACE + if value != Namespace::XML_URI msg = "The 'xml' prefix must not be bound to any other namespace "+ "(http://www.w3.org/TR/REC-xml-names/#ns-decl)" raise REXML::ParseException.new( msg, @source, self ) diff --git a/lib/rexml/xpath_parser.rb b/lib/rexml/xpath_parser.rb index dbe2c53a..20ac443b 100644 --- a/lib/rexml/xpath_parser.rb +++ b/lib/rexml/xpath_parser.rb @@ -183,7 +183,7 @@ def strict? # 2. If no mapping was supplied, use the context node to look up the namespace as a fallback. def get_xpath_namespace(node, prefix) if @namespaces - @namespaces[prefix] || '' + Namespace.resolve_prefix(prefix, @namespaces) || '' elsif node.node_type == :element element_namespace_lookup(node, prefix) else @@ -733,10 +733,10 @@ def node_test(path_stack, any_type: :element) case node.node_type when :element namespaces = @namespaces || element_namespaces(node) - element_namespace(node) == namespaces[prefix] + element_namespace(node) == Namespace.resolve_prefix(prefix, namespaces) when :attribute namespaces = @namespaces || element_namespaces(node.element) - attribute_namespace(node) == namespaces[prefix] + attribute_namespace(node) == Namespace.resolve_prefix(prefix, namespaces) else false end @@ -786,14 +786,8 @@ def evaluate_predicate(expression, nodesets) if result == position new_nodeset << node end - elsif result.instance_of? Array - if result.size > 0 - new_nodeset << node - end - else - if result - new_nodeset << node - end + elsif @functions.boolean(result) + new_nodeset << node end end new_nodeset diff --git a/test/xpath/test_attribute.rb b/test/xpath/test_attribute.rb index e57bd915..3d6342c5 100644 --- a/test/xpath/test_attribute.rb +++ b/test/xpath/test_attribute.rb @@ -25,6 +25,18 @@ def test_xpath_each assert_equal(["child2"], children.collect(&:text)) end + def test_predefined_xml_namespace + doc = REXML::Document.new('') + uri = "http://www.w3.org/XML/1998/namespace" + attribute = doc.root.attributes.get_attribute("xml:lang") + + assert_equal([attribute], REXML::XPath.match(doc, "//@xml:*", "xml" => uri)) + assert_equal([attribute], REXML::XPath.match(doc, "//@xml:*")) + assert_equal([attribute], REXML::XPath.match(doc, "//@x:lang", "x" => uri)) + assert_equal([attribute], REXML::XPath.match(doc, "//@xml:lang", {})) + assert_equal([uri], REXML::XPath.match(doc, "namespace-uri(//@xml:lang)")) + end + def test_no_namespace children = REXML::XPath.match(@document, "/root/child[@nothing:name='two']", diff --git a/test/xpath/test_predicate.rb b/test/xpath/test_predicate.rb index 7cafc49c..86df63ed 100644 --- a/test/xpath/test_predicate.rb +++ b/test/xpath/test_predicate.rb @@ -87,6 +87,12 @@ def do_path( path ) return m end + def test_string_predicate_uses_xpath_boolean_value + doc = REXML::Document.new(" 0") + matches = REXML::XPath.match(doc, "/r/a/text()[normalize-space(.)]") + assert_equal(["0"], matches.map(&:value)) + end + def test_predicate_float_literal doc = REXML::Document.new("") # [N.0] is equivalent to [position() = N.0] = [position() = N]