On Sun, Aug 02, 2009 at 12:50:05AM +0900, Matt N. wrote:
include REXML
doc = Document.new(s)
p XPath.match(doc, “//treenode[‘Root’]/treenode”)
#=> []
Wow. These results are just wrong. This is a bug in REXML. In XPath,
when you do not specify a namespace for your node, that means that you
want a node with no namespace.
For example:
require ‘rexml/document’
include REXML
s = <<END
<?xml version="1.0" encoding="UTF-8"?>
<!-- bike inventory -->
<inventory xmlns="http://schwinn.com/">
<tire name="street" />
</inventory>
<!-- no namespace inventory -->
<inventory>
<tire name="wtf" />
</inventory>
END
doc = Document.new(s)
p XPath.match(doc, “//tire”)
REXML matches all three tires. Surely a car tire is not the same as a
bike
tire? Using XPath, how would I query for a tire that has no namespace
(the third one) without matching the two that do belong in a
namespace (it’s possible to do this with REXML, just strange)? The
XPath used
above should only match the third entry.
This is a broken implementation of XPath.
Oh, wait, you said you were using libxml:
You have an error in your XML below
s = <<END
<?xml version="1.0" encoding="UTF-8"?>
^ That ">" should not be there.
libxml-ruby has corrections turned on by default, so you’ve effectively
removed all namespaces from this document.
end
Sorry, I’m failing to guess what problem you’re having. Perhaps if you
showed your actual code? m.
Since the namespaces were removed, this example succeeds.