Ticket #121 (closed defect: fixed)

Opened 13 months ago

Last modified 12 months ago

Cannot select attribute with phantom namespace pointing to default namespace

Reported by: sashkap Owned by: rubys
Priority: normal Milestone:
Component: DOM Version: 3.1.4
Severity: normal Keywords:
Cc: Ruby version: Other
Operating system: Linux

Description

This is related to ticket #102, posted by kevinj.

In my case I have to parse a file produced by XSLT translation, and the XSLT processor leaves a phantom namespace in the root element.

After that I cannot extract attribute from the elements of this document.

Test case:

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

Expected results:

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

Actual results with ruby 1.8.6:

$ ruby test.rb 
nil
nil
nil

The following re implementation of REXML::Element.attribute method in /usr/lib/ruby/1.8/rexml/element.rb fixes this problem

def attribute( name, namespace=nil )
  prefix = nil
  prefix = namespaces.index(namespace) if namespace
  prefix = nil if prefix == 'xmlns'
  
  ret_val =
    attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" )

  return ret_val unless ret_val.nil?

  return nil if prefix.nil?
  
  # now check that prefix'es namespace is not the same as the
  # default namespace
  return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )
  
  attributes.get_attribute( name )
end

Change History

Changed 12 months ago by rubys

  • owner changed from ser to rubys

Changed 12 months ago by rubys

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

Changed 12 months ago by rubys

Note: See TracTickets for help on using tickets.