From fdecae6b0395412930ffca1b265a20b768a821bd Mon Sep 17 00:00:00 2001 From: Wolfgang Woehl Date: Mon, 6 May 2013 20:18:36 +0200 Subject: [PATCH] dcp_inspect: Handle multiple default namespaces See https://github.com/sparklemotion/nokogiri/issues/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. --- dcp_inspect | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/dcp_inspect b/dcp_inspect index a81eae8..c91b5f4 100755 --- a/dcp_inspect +++ b/dcp_inspect @@ -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 )