Skip to content

Commit

Permalink
Add Nokogiri::XML::Document#collect_namespaces_[href_keys|prefix_keys]
Browse files Browse the repository at this point in the history
See sparklemotion#885 for details
  • Loading branch information
wolfgangw committed Nov 13, 2013
1 parent 13e4307 commit 2613e70
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/nokogiri/xml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ def collect_namespaces
end
end

##
# In order to collect all namespaces in a document with multiple
# default namespace definitions use
# - collect_namespaces_href_keys
# - collect_namespaces_prefix_keys
#
# See https://github.com/sparklemotion/nokogiri/issues/885 for details
#
# collect_namespaces_href_keys will return e.g.
#
# {
# "http://www.w3.org/XML/1998/namespace" => ["xml"],
# "http://www.smpte-ra.org/schemas/429-7/2006/CPL" => [nil],
# "http://isdcf.com/schemas/draft/2011/cpl-metadata" => ["meta"],
# "http://www.w3.org/2000/09/xmldsig#" => [nil]
# }
#
# where nil implies xmlns.
#
# collect_namespaces_prefix_keys will return e.g.
#
# {
# "xml" => ["http://www.w3.org/XML/1998/namespace"],
# "nil" => ["http://www.smpte-ra.org/schemas/429-7/2006/CPL", "http://www.w3.org/2000/09/xmldsig#"],
# "meta" => ["http://isdcf.com/schemas/draft/2011/cpl-metadata"]
# }
#
# where nil implies xmlns.
#
def collect_namespaces_href_keys
xpath("//namespace::*").inject({}) do |hash, ns|
(hash[ ns.href ] ||= []) << ns.prefix unless (hash[ ns.href ] and hash[ ns.href ].include? ns.prefix)
hash
end
end
def collect_namespaces_prefix_keys
xpath("//namespace::*").inject({}) do |hash, ns|
(hash[ ns.prefix ] ||= []) << ns.href unless (hash[ ns.prefix ] and hash[ ns.prefix ].include? ns.href)
hash

This comment has been minimized.

Copy link
@leejarvis

leejarvis Jan 24, 2014

I think we could use each_with_object to avoid adding this line

end
end

# Get the list of decorators given +key+
def decorators key
@decorators ||= Hash.new
Expand Down

1 comment on commit 2613e70

@leejarvis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for these methods is good but it needs to be separate for each method for it reflects accordingly in the rdoc

Please sign in to comment.