Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rexml/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/rexml/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative '../source'
require_relative '../text'
require_relative '../xmltokens'
require_relative '../namespace'
require 'set'
require "strscan"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -201,7 +201,7 @@ def reset
@tags = []
@stack = []
@entities = []
@namespaces = {"xml" => Private::XML_PREFIXED_NAMESPACE}
@namespaces = {"xml" => Namespace::XML_URI}
@namespaces_restore_stack = []
end

Expand Down Expand Up @@ -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 )
Expand Down
16 changes: 5 additions & 11 deletions lib/rexml/xpath_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/xpath/test_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def test_xpath_each
assert_equal(["child2"], children.collect(&:text))
end

def test_predefined_xml_namespace
doc = REXML::Document.new('<r lang="other" xml:lang="en"/>')
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']",
Expand Down
6 changes: 6 additions & 0 deletions test/xpath/test_predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def do_path( path )
return m
end

def test_string_predicate_uses_xpath_boolean_value
doc = REXML::Document.new("<r><a> </a><a>0</a></r>")
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("<r><a/><b/><c/><d/></r>")
# [N.0] is equivalent to [position() = N.0] = [position() = N]
Expand Down
Loading