lib/happymapper.rb in nokogiri-happymapper-0.3.3 vs lib/happymapper.rb in nokogiri-happymapper-0.3.4
- old
+ new
@@ -91,29 +91,39 @@
root = node.name == tag_name
end
# This is the entry point into the parsing pipeline, so the default
# namespace prefix registered here will propagate down
- namespaces = options[:namespaces] || xml.namespaces
+ namespaces = options[:namespaces]
+ namespaces ||= {}
+ namespaces = namespaces.merge(xml.collect_namespaces) if xml.respond_to?(:collect_namespaces)
+ namespaces = namespaces.merge(xml.namespaces)
+
if namespaces.has_key?("xmlns")
namespace ||= DEFAULT_NS
namespaces[namespace] = namespaces.delete("xmlns")
elsif namespaces.has_key?(DEFAULT_NS)
namespace ||= DEFAULT_NS
end
- xpath = root ? '/' : './/'
- xpath += "#{namespace}:" if namespace
- #puts "parse: #{xpath}"
+ nodes = options.fetch(:nodes) do
+ xpath = (root ? '/' : './/')
+ xpath = options[:xpath].to_s.sub(/([^\/])$/, '\1/') if options[:xpath]
+ xpath += "#{namespace}:" if namespace
+ #puts "parse: #{xpath}"
- nodes = []
- # when finding nodes, do it in this order:
- # 1. specified tag
- # 2. name of element
- # 3. tag_name (derived from class name by default)
- [options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
- nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
- break if nodes && nodes.size > 0
+ nodes = []
+
+ # when finding nodes, do it in this order:
+ # 1. specified tag
+ # 2. name of element
+ # 3. tag_name (derived from class name by default)
+ [options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
+ nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
+ break if nodes && !nodes.empty?
+ end
+
+ nodes
end
collection = nodes.collect do |n|
obj = new