Warning: Can't synchronize with the repository (Unsupported version control system "hg": "Couldn't import symbols: RepoError" ). Look in the Trac log for more information.

Ticket #102 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

Cannot select attribute with default namespace

Reported by: kevinj Owned by: ser
Priority: normal Milestone: 3.1.8
Component: DOM Version: 3.1.6
Severity: normal Keywords:
Cc: Ruby version: Other
Operating system: Linux

Description

In ruby 1.8.6, Element::attribute returns nil rather than the attribute when called with the default namespace:

Test case:

require 'rexml/document'

doc = REXML::Document.new '<doc xmlns="ns"><item name="foo"/></doc>' p doc.root.elementsitem?.attribute("name", "ns") p doc.root.elementsitem[@name='foo']?

Output with ruby 1.8.5:

$ ruby test.rb

name='foo' <item name='foo'/>

Output with ruby 1.8.6:

$ ruby test.rb

nil nil

The problem appears to have been introduced with this changeset: http://www.germane-software.com/projects/rexml/changeset/1237

The below patch seems to fix the issue:

--- /usr/lib/ruby/1.8/rexml/element.rb.bak 2007-05-08 11:18:39.000000000 -0700

+++ /usr/lib/ruby/1.8/rexml/element.rb 2007-05-08 11:18:41.000000000 -0700 @@ -558,6 +558,7 @@

def attribute( name, namespace=nil )

prefix = nil

prefix = namespaces.index(namespace) if namespace

+ prefix = nil if prefix == 'xmlns'

attributes.get_attribute( "#{prefix ? prefix + ':' : }#{name}" )

end

Change History

Changed 2 years ago by kevinj

Oops, sorry about the formatting. Here's a more readable description:

Test case:

require 'rexml/document'
doc = REXML::Document.new '<doc xmlns="ns"><item name="foo"/></doc>'
p doc.root.elements["item"].attribute("name", "ns")
p doc.root.elements["item[@name='foo']"]

Output with ruby 1.8.5:

$ ruby test.rb
name='foo'
<item name='foo'/>

Output with ruby 1.8.6:

$ ruby test.rb 
nil
nil

This appears to have been introduced with this changeset: http://www.germane-software.com/projects/rexml/changeset/1237

The below patch seems to fix the issue:

--- /usr/lib/ruby/1.8/rexml/element.rb.bak      2007-05-08 11:18:39.000000000 -0700
+++ /usr/lib/ruby/1.8/rexml/element.rb  2007-05-08 11:18:41.000000000 -0700
@@ -558,6 +558,7 @@
                def attribute( name, namespace=nil )
                        prefix = nil
       prefix = namespaces.index(namespace) if namespace
+      prefix = nil if prefix == 'xmlns'
                        attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )
                end

Changed 2 years ago by ser

  • milestone set to 3.1.8

Changed 2 years ago by ser

  • status changed from new to assigned

Changed 2 years ago by ser

  • status changed from assigned to closed
  • resolution set to fixed

Fixed by changeset:1278.

Thanks for the analysis and the solution.

Note: See TracTickets for help on using tickets.