Skip to content

Element#attribute fails to find attribute when namespace URL is duplicated #346

Description

@tompng
xml = "<root xmlns='url1' xmlns:ns1='url1' xmlns:ns2='url2' xmlns:ns3='url2' a='' ns1:a='' ns1:b='' ns2:c='' ns3:d=''/>"
node = REXML::Document.new(xml).root

node.attribute 'a', 'url1'
# => a='' (ns1:a should match)
node.attribute 'b', 'url1'
# => nil (ns1:b='' should match)
node.attribute 'c', 'url2'
# => ns2:c='' (OK)
node.attribute 'd', 'url2'
# => nil (ns3:d='' should match)

# Ref:
node.attributes.get_attribute_ns '', 'a'     #=> a=''
node.attributes.get_attribute_ns 'url1', 'a' #=> ns1:a=''
node.attributes.get_attribute_ns 'url1', 'b' #=> ns1:b=''
node.attributes.get_attribute_ns 'url2', 'c' #=> ns2:c=''
node.attributes.get_attribute_ns 'url2', 'd' #=> ns3:d=''

Result of node.attribute 'b', 'url1' has changed in #335 as a bugfix (DOM spec violation) of Attributes#get_namespace.
node.attribute 'a', 'url1' case is related to #151.

The easiest way to fix this is to simply use:

def attribute( name, namespace=nil )
  if namespace
    attributes.get_attribute_ns(namespace, name)
  else
    attributes.get_attribute(name)
  end
end

However, it may introduce incompatibility mentioned in #151.
If this method is fixed as above, I think we don't need to deprecate this method (discussed in #151) because it will be a useful method: a superset of attributs.get_attribute_ns and attributs.get_attribute.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions