Created
October 26, 2010 21:05
-
-
Save jrochkind/647802 to your computer and use it in GitHub Desktop.
XML::Document#xpath inconsistent use of assumed namespaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
xml = %Q{ | |
<root xmlns:example="http://example.org"> | |
<example:element> | |
<example:child> | |
</example:element> | |
</root> | |
} | |
doc = Nokogiri::XML(xml) | |
# 1 | |
puts | |
assumed_namespace_on_root = doc.root.xpath("example:element") | |
# found it, no problem | |
puts "doc.root.xpath('example:element') => #{assumed_namespace_on_root}" | |
# 2 | |
puts | |
begin | |
unspecified_namespace_on_root = doc.root.xpath('bad:element') | |
rescue Nokogiri::XML::XPath::SyntaxError => e | |
puts "doc.root.xpath('bad:element') raises exception, great: #{e}" | |
end | |
# 3 | |
puts | |
begin | |
unspecified_namespace_on_document = doc.xpath('bad:element') | |
rescue Nokogiri::XML::XPath::SyntaxError => e | |
puts "doc.xpath('bad:element') still raises exception, great: #{e}" | |
end | |
# 4 | |
puts | |
assumed_namespace_on_doc = doc.xpath("example:element") | |
puts %Q{Calling doc.xpath('example:element') should: | |
* EITHER use the implicit namespaces on doc.root, and return the same thing as doc.root.xpath('example:element') as in #1 (This is my preference) | |
* OR raise an exception for unknown namespace as in #2 and #3. | |
Instead, it simply returns an empty set, that does not seem right, it's not raising unspecified namespace, but is ALSO not succesfully using the namespace, this is a recipe for confusion: | |
} | |
puts assumed_namespace_on_doc.inspect # => [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment