Nokogiri を使用すると、次のコードを記述できます。
require 'inflector'
require 'nokogiri'
def get_xml_stuff(xml, singular)
plural = Inflector.pluralize(singular)
return_hash = {plural => []}
xml.xpath("*/#{plural}/#{singular}").each { |tag| return_hash[plural] << tag.text}
return return_hash
end
私のテストによると、これは XmlSimple コードに一致する単純なケースを解決します。あなたのさらなる要件について:
nodexyzs
に名前付きの 1 つ以上の子ノードが含まれるxyz
(他のノードは含まれない) 場合、 nodexyzs
は、結果のハッシュで名前付きの配列として表される必要がありますxyzs
(配列の各要素は、対応する要素の内容である必要がありますxyz
)。
def get_xml_stuff(xml, singular)
plural = Inflector.pluralize(singular)
return_hash = {plural => []}
path = xml.xpath("*/#{plural}/#{singular}")
path.each { |tag| return_hash[plural] << tag.text} unless path.size != xml.xpath("*/#{plural}/*").children.size
return return_hash
end
ただし、同じ複数形がファイルに複数回出現する場合は、まだ完全ではありません。
UPD2に応答しています。関数の新しいバージョンは次のとおりです。
def get_xml_stuff(xml, plural)
singular = Inflector.singularize(plural)
return_hash = {plural => []}
path = xml.xpath("./#{singular}")
path.each { |tag| return_hash[plural] << tag.text} unless path.size != xml.xpath("./*").size
return return_hash
end
ここでは、複数形の親ノードから開始し、名前付きのすべての子ノードがその単数形の名前である場合、すべての単数形の子ノードを収集します。私の新しいテストコードは次のようになります。
sample_xml = Nokogiri::XML(sample_xml_text)
sample_xml.children.xpath("*").each do |child|
array = get_xml_stuff(child, child.name)
p array
end
私の例のようなタグがない場合<pets>
、次のように動作するはずです:
sample_xml = Nokogiri::XML(sample_xml_text)
array = get_xml_stuff(sample_xml.children.first, sample_xml.children.first.name)
p array
UPD2 の終了
参考までに、私のテストは次のとおりです。
sample_xml_text = <<-sample
<pets>
<cats>
<cat>John</cat>
<cat>Peter</cat>
</cats>
<kitties>
<kitty>Tibbles</kitty>
<kitty>Meow-chan</kitty>
<kitty>Puss</kitty>
</kitties>
<giraffes>
<giraffe>Long Neck</giraffe>
</giraffes>
<dogs>
<dog>Rover</dog>
<dog>Spot</dog>
<cat>Peter</cat>
</dogs>
</pets>
sample
sample_xml = Nokogiri::XML(sample_xml_text)
array = get_xml_stuff(sample_xml, "cat")
p array
array = get_xml_stuff(sample_xml, "kitty")
p array
array = get_xml_stuff(sample_xml, "giraffe")
p array
array = get_xml_stuff(sample_xml, "dog")
p array