使用 Nokogiri::XML#collect_namespaces 还是 #namespaces?

Use Nokogiri::XML#collect_namespaces or #namespaces?

我发现 Nokogiri::XML 有两种获取命名空间列表的方法:#namespacescollect_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"

这两种方法的工作原理几乎相同,除了一个 returns 哈希的倒序。这有什么原因吗?

我无法获得应该使用哪种方法的信息。

如果除了顺序没有区别,我会用namespaces,因为它更短。

collect_namespacesDocument 获取所有命名空间。 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`