2016-03-05 73 views
0

我发现,引入nokogiri :: XML有两种方法来获取命名空间的列表:#namespacescollect_namespaces使用Nokogiri :: XML#collect_namespaces或#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,因为它更短。

回答

1

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`