Search code examples
rubyxpathrexml

REXML fails to select from attribute. Bug or incorrect XPath?


I try to select an element from an SVG document by a special attribute. I set up a simple example.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg">
  <g id='1'>
    <path id='2' type='A'/>
    <rect id='3' type='B'/>
  </g>
</svg>

Now I use the following syntax to retrieve the path element by its attribute "type":

require 'rexml/document'
include REXML
xmlfile = File.new "xml_as_specified_above.svg"
xmldoc = Document.new(xmlfile)
XPath.match( xmldoc.root, "//path[@type]" )

Syntax directly from http://www.w3schools.com/xpath/xpath_syntax.asp. I would expect that this expression selects the path element but this is what follows:

>> XPath.match( xmldoc.root, "//path[@type]" )
=> []

So, what is the correct syntax in XPath to address the path element by it's attribute? Or is there a bug in REXML (using 3.1.7.3)? Plus points for also retrieving the "rect" element.


Solution

  • It looks like an older version of rexml is being picked up that doesn't support the full XPath spec.

    Try checking the output of puts XPath::VERSION to ensure that 3.1.73 is displayed.