nokogiri id 中的每个属性 - @id 在两个节点中

nokogiri each attribute in id - @id in both nodes

我有一个遵循这个结构的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>

我希望能够提取提名 ID 属性并将其与它来自的种族 ID 相关联,以便打印输出,我真的会稍后将其放入数据库。

RaceID    NomID
215411    198926
215411    198965
215411    199260
215412    199634
215412    208926
215412    122923

尝试了一些不同的路线,但无法使用 css 选择器(例如 @doc.css('race id') 使其工作,实际上收集了文档中的所有后代 ID比赛。

这是我到目前为止所到之处

require 'nokogiri'

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

race.each do |f|
  f.xpath('//@id | //nomination/@id').each do |node|
    puts node['V']
  end
end
#node_num = race_id.length
#(1..node_num).each do |x|
  #nom_id.each do |y|
    #puts "race ID\t" + "#{race_id[x]}  " + "nom_id\t" + "#{y}"
#end
#end

不知道很酷的选择器,但你可以这样做

races = xml.xpath('//race')
races.map do |race|
  race_id = race.xpath('./@id').text.to_i
  nomination_ids = race.xpath('./nomination/@id').map { |id| id.text.to_i }
  nomination_ids.map do |nomination_id|
    { race_id: race_id, nomination_id: nomination_id }
  end
end.flatten

这将 return 一个 array 的哈希值,例如

[
  {:race_id => 215411, :nomination_id => 198926},
  {:race_id => 215411, :nomination_id => 198965},
  {:race_id => 215411, :nomination_id => 199260},
  {:race_id => 215412, :nomination_id => 199634},
  {:race_id => 215412, :nomination_id => 208926},
  {:race_id => 215412, :nomination_id => 122923}
]

我更喜欢使用 hash 之类的导出方式,所以你可以使用类似这样的方式。

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 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>'

require 'nokogiri'

doc = Nokogiri::XML(xml)

races = doc.xpath("//race")

export = races.each_with_object(Hash.new { |k, v| k[v] = [] }) do |elem, exp|
  elem.xpath("./nomination").each do |nom_elem|
    exp[elem['id']] << nom_elem['id']
  end
end

输出

p export

# {
#   "215411"=>["198926", "198965", "199260"],
#   "215412"=>["199634", "208926", "122923"]
# }

希望对您有所帮助

我只是尝试另一种只有一个循环的解决方案。

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 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>'

require 'nokogiri'

doc = Nokogiri::XML(xml)

nominations = doc.xpath("//nomination")

export = nominations.each_with_object(Hash.new { |k, v| k[v] = [] }) do |non_elem, exp|
  exp[non_elem.parent['id']] << non_elem['id']
end

输出

p export

# {
#   "215411"=>["198926", "198965", "199260"],
#   "215412"=>["199634", "208926", "122923"]
# }