我发现Nokogiri::XML有两种方法来获取命名空间列表:#namespaces
和collect_namespaces
:
doc.namespaces
{
"xmlns:iso4217" => "http://www.xbrl.org/2003/iso4217",
"xmlns:link" => "http://www.xbrl.org/2003/linkbase",
"xmlns:tdnet-qcedjpsm-99970" => "http://www.xbrl.tdnet.info/jp/br/tdnet/qc/edjp/sm/99970/2013-08-02399970",
"xmlns:tse-t-ed" => "http://www.xbrl.tdnet.info/jp/br/tdnet/t/ed/2007-06-30",
"xmlns:tse-t-hi" => "http://www.xbrl.tdnet.info/jp/br/tdnet/t/hi/2007-06-30",
"xmlns:xlink" => "http://www.w3.org/1999/xlink",
"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xbrli" => "http://www.xbrl.org/2003/instance"
}
doc.collect_namespaces
{
"xmlns:xbrli" => "http://www.xbrl.org/2003/instance",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
"xmlns:xlink" => "http://www.w3.org/1999/xlink",
"xmlns:tse-t-hi" => "http://www.xbrl.tdnet.info/jp/br/tdnet/t/hi/2007-06-30",
"xmlns:tse-t-ed" => "http://www.xbrl.tdnet.info/jp/br/tdnet/t/ed/2007-06-30",
"xmlns:tdnet-qcedjpsm-99970" => "http://www.xbrl.tdnet.info/jp/br/tdnet/qc/edjp/sm/99970/2013-08-02399970",
"xmlns:link" => "http://www.xbrl.org/2003/linkbase",
"xmlns:iso4217" => "http://www.xbrl.org/2003/iso4217"
这两种方法的工作方式几乎相同,除了一种返回哈希的反顺序。 这有什么原因吗?
我无法获得应该使用哪种方法的信息。
如果除了顺序之外没有区别,我将使用 namespaces
,因为它更短。
collect_namespaces
从 Document
获取所有命名空间。 namespaces
获取对Node
有效的命名空间。
d = Nokogiri::XML(<<XML)
<a xmlns:a="http://example.com/a">
<b xmlns:b="http://example.com/b">
</b>
</a>
XML
d.namespaces
# => {"xmlns:a"=>"http://example.com/a"}
# because `xmlns:b` is below the root, it is not in effect
d.collect_namespaces
# => {"xmlns:a"=>"http://example.com/a", "xmlns:b"=>"http://example.com/b"}
# gets everything
d.at_css('b').namespaces
# => {"xmlns:b"=>"http://example.com/b", "xmlns:a"=>"http://example.com/a"}
# on the child, both namespaces are in effect
d.at_css('b').collect_namespaces
# => NoMethodError
# because `collect_namespaces` only works on `Document`