gpt4 book ai didi

ruby - 如何基于属性从XML获取后代节点

转载 作者:行者123 更新时间:2023-12-03 17:29:51 25 4
gpt4 key购买 nike

我正在尝试获取节点的后代子代:

require 'nokogiri'

@doc = Nokogiri::XML(File.open('data/20160521RHIL0.xml'))
nom_id = @doc.xpath('//race/nomination/@id')

race_id.each do |x|
puts race_id.traverse {|race_id| puts nom_id }
end


我正在查看两种信息来源:


XML:Node的文档,其中包含

Nokogiri::XML::Node#children

sparklemotion的 Cheat-sheet

node.traverse {|node| } # yields all children and self to a block, _recursivel





这是我的测试XML:

<meeting id="42977">
<race id="215411">
<nomination number="8" saddlecloth="8" horse="Chipanda" id="198926" />
<nomination number="2" saddlecloth="2" horse="Chifries" id="198965" />
<nomination number="1" saddlecloth="1" horse="Itpanda" id="199260" />
</race>
<race id="215412">
<nomination number="1" saddlecloth="1" horse="Ruby" id="199634" />
<nomination number="2" saddlecloth="2" horse="Gems" id="208926" />
<nomination number="3" saddlecloth="3" horse="Rock" id="122923" />
</race>
</meeting>


我可以使用XPath轻松获得竞赛 id

require 'nokogiri'                                                                                                                      

@doc = Nokogiri::XML(File.open('data/20160521RHIL0.xml'))

race_id = @doc.xpath('//race/@id')
nom_id = @doc.xpath('//race/nomination/@id')

...
215411
215412


如何获取节点提名ID和 race_id 215411的编号并将其存储到哈希(如下所示)中?

{215411 => [{id:198926, number:8},{id:198965, number:2}]}

最佳答案

require 'nokogiri'

# xml data
str =<<-EOS
<meeting id="42977">
<race id="215411">
<nomination number="8" saddlecloth="8" horse="Chipanda" id="198926" />
<nomination number="2" saddlecloth="2" horse="Chifries" id="198965" />
<nomination number="1" saddlecloth="1" horse="Itpanda" id="199260" />
</race>
<race id="215412">
<nomination number="1" saddlecloth="1" horse="Ruby" id="199634" />
<nomination number="2" saddlecloth="2" horse="Gems" id="208926" />
<nomination number="3" saddlecloth="3" horse="Rock" id="122923" />
</race>
</meeting>
EOS

# create doc
doc = Nokogiri::XML(str)

# clean; via http://stackoverflow.com/a/1528247
doc.xpath('//text()[not(normalize-space())]').remove

# parse doc
parsed_doc = doc.xpath('//race').inject({}) {|h,x| h[x.get_attribute('id').to_i] = x.children.map {|y| {id: y.get_attribute('id').to_i, number: y.get_attribute('number').to_i}}; h}
# {215411=>
# [{:id=>198926, :number=>8},
# {:id=>198965, :number=>2},
# {:id=>199260, :number=>1}],
# 215412=>
# [{:id=>199634, :number=>1},
# {:id=>208926, :number=>2},
# {:id=>122923, :number=>3}]}

# select via id
parsed_doc.select {|k,v| k == 215411}
# {215411=>
# [{:id=>198926, :number=>8},
# {:id=>198965, :number=>2},
# {:id=>199260, :number=>1}]}


这是单线多线:

parsed_doc = doc.xpath('//race').inject({}) do |h,x|
h[x.get_attribute('id').to_i] = x.children.map do |y|
{
id: y.get_attribute('id').to_i,
number: y.get_attribute('number').to_i
}
end
h
end

关于ruby - 如何基于属性从XML获取后代节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com