Skip to content

Commit

Permalink
dcp_inspect: Handle multiple default namespaces
Browse files Browse the repository at this point in the history
See sparklemotion/nokogiri#885 for details

This adds

  Nokogiri::XML::Document#collect_all_namespaces_href_keys
  Nokogiri::XML::Document#collect_all_namespaces_prefix_keys

which allow to actually collect all of a document's namespaces
and namespace prefixes.
  • Loading branch information
wolfgangw committed May 6, 2013
1 parent 3dad653 commit fdecae6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions dcp_inspect
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,46 @@ class DLogger
end


# Workaround Nokogiri::XML::Document#collect_namespaces peculiarity
# (see https://github.com/sparklemotion/nokogiri/issues/885 for details)
#
# collect_all_namespaces_href_keys will return
#
# {
# "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.
# This is an example where multiple default namespace definitions exist
# and are used in different document fragments.
#
# collect_all_namespaces_prefix_keys will return
#
# {
# "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"]
# }
#
class Nokogiri::XML::Document
def collect_all_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_all_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
end
end
end


# lib/mxf.rb
module MxfTools
def mxf_inspect( filename )
Expand Down

0 comments on commit fdecae6

Please sign in to comment.