如何使用XPath与Nokogiri根据Tagname从Nodeset中选择单个元素

时间:2012-01-08 20:02:42

标签: ruby xml xpath nokogiri

给出以下XML,

<Container>
<Set >
<RecommendedCoverSong>Hurt by NiN - Johnny Cash</RecommendedCoverSong>
<RecommendedOriginalSong>She Like Electric by Smoosh</RecommendedOriginalSong>
<RecommendedDuetSong>Portland by Jack White and Loretta Lynn</RecommendedDuetSong>
<RecommendedGroupSong>SoS by Abba</RecommendedGroupSong>
<CoverSong>Kangaroo  by Big Star  - This Mortal Coil</CoverSong>
<OriginalSong>Pick up the Change by Wilco</OriginalSong>
<DuetSong>I am the Cosmos by Pete Yorn and Scarlett Johansen</DuetSong>
<GroupSong>Kitties Never Rest by Rex or Regina</GroupSong>
</Set>
</Container>

我想在标签中抓取两个包含“封面”的元素,然后对它们进行操作。

Nokogiri使用Xpath轻松允许第一个查询表达式如下:

price_xml = doc_xml.xpath('Container/Set/*[contains(name(), "Cover")]')

我在Set中选择了所有元素(使用*),然后使用了Xpath Expression函数:

包含,以指定Adult必须在名称中。这将在Nodeset中返回两个Nokogiri XML节点。

我想要做的是根据标记名中的模式选择其中一个元素,使用我最喜欢的工具Xpath。

但是我无法让Nokogiri把它交给我,并且有几种解决方案最终选择的方式超过了我想要的1个元素。 (因为Nodeset中的节点仍然包含与其父节点的关系)

songtypes = ['Cover', 'Original', 'Duet', 'Group']
songtypes.each do |song|

node_xml = doc.xpath('Container/Set/*[contains(name(), "Cover")]')
#I wanted to be able to do the following
#
FavoriteCover =  node_xml.xpath('./*[contains(name(), "Recommended")]')
RegularCover  =  node_xml.xpath('./*[not(contains(name(), "Recommended"))]')

#or
FavoriteCover =  node_xml.xpath('*[contains(name(), "Recommended")]')
RegularCover  =  node_xml.xpath('*[not(contains(name(), "Recommended"))]')
#But instead I had to resort to a Rails solution

RegularCover  =  node_xml.find{ |node| node.name !~ /Recommended/ }
FavoriteCover =  node_xml.find{ |node| node.name =~ /Recommended/ }

#Do something with the songs here

end

https://gist.github.com/1579343

1 个答案:

答案 0 :(得分:1)

尝试类似:

node_xml.at_xpath('./self::*[not(contains(name(), "Recommended"))]')
node_xml.at_xpath('./self::*[contains(name(), "Recommended")]')

考虑在迭代中使用变量而不是常量。

或者您可以生成节点名称:

songtypes = ['Cover', 'Original', 'Duet', 'Group']
songtypes.each do |st|
  regular = doc.at_xpath("Container/Set/#{st}Song")
  recommended = doc.at_xpath("Container/Set/Recommended#{st}Song")
end