ruby - Xpath Nokogiri嵌套

标签 ruby xpath nokogiri

我正在为一个包含计算机游戏产品信息的联属网络营销计划处理大型 XML 文档。整体示例如下所示。

<prod id="743854322">
    <pId>GS811CF</pId>
    <text>
        <name>Tour De France 2013</name>
        <desc>Platform: XBOX 360  Publisher: FOCUS HOME INTER  Genre: SPORTS  Supported Languages: English</desc>
    </text>
    <uri>
        <awTrack>http://www.awin1.com/pclick.php?p=743854322&amp;a=161542&amp;m=3026</awTrack>
        <awImage>http://images.productserve.com/preview/3026/743854322.jpg</awImage>
        <mLink>http://tracking.searchmarketing.com/click.asp?aid=520005430000038657</mLink>
        <mImage>http://images2.drct2u.com/content/images/products/gs/gs811/gs811_xb2to52.jpg</mImage>
    </uri>
    <price curr="GBP">
        <buynow>47.00</buynow>
        <delivery>3.99</delivery>
    </price>
    <cat>
        <awCatId>579</awCatId>
        <awCat>Video Games</awCat><mCat>Main Menu|Electricals|Gaming &amp;amp; Consoles|Video Games</mCat>
    </cat>
    <brand>
        <awBrandId>427</awBrandId>
        <brandName>Xbox 360</brandName>
    </brand>
</prod>

我希望能够在此文档中搜索等于“Tour De France 2013”​​并且也在 xbox“平台:XBOX 360”上的计算机游戏名称。

我已经用 xpath 尝试了一些东西,但似乎无法达到我想要的效果。我想知道你们会怎么做。

我试过了(这行得通...但我不想对“名称”使用 contains 方法)

result = file.xpath("//prod[contains(.,\"Tour De France 2013\") and contains(.,\"Platform: XBOX 360\")]")[0]
# => #<Nokogiri::XML::Element:0x2b6e23c name="prod" attributes=[#<Nokogiri::XML::Attr:0x2b6e19c name="id" value="743854322">] children=[#<Nokogiri::XML::Element:0x2b6db84 name="pId" children=[#<Nokogiri::XML::Text:0x2b6d850 "GS811CF">]>, #<Nokogiri::XML::Element:0x2b6d5d0 name="text" children=[#<Nokogiri::XML::Element:0x2b6d300 name="name" children=[#<Nokogiri::XML::Text:0x2b6d094 "Tour De France 2013">]>, #<Nokogiri::XML::Element:0x2b6ce14 name="desc" children=[#<Nokogiri::XML::Text:0x2b6cb1c "Platform: XBOX 360  Publisher: FOCUS HOME INTER  Genre: SPORTS  Supported Languages: English">]>]>, #<Nokogiri::XML::Element:0x2b6c680 name="uri" children=[#<Nokogiri::XML::Element:0x2b70334 name="awTrack" children=[#<Nokogiri::XML::Text:0x2b70078 "http://www.awin1.com/pclick.php?p=743854322&a=161542&m=3026">]>, #<Nokogiri::XML::Element:0x2b6fd94 name="awImage" children=[#<Nokogiri::XML::Text:0x2b6fb14 "http://images.productserve.com/preview/3026/743854322.jpg">]>, #<Nokogiri::XML::Element:0x2b6f81c name="mLink" children=[#<Nokogiri::XML::Text:0x2b6f510 "http://tracking.searchmarketing.com/click.asp?aid=520005430000038657">]>, #<Nokogiri::XML::Element:0x2b6f290 name="mImage" children=[#<Nokogiri::XML::Text:0x2b6efd4 "http://images2.drct2u.com/content/images/products/gs/gs811/gs811_xb2to52.jpg">]>]>, #<Nokogiri::XML::Element:0x2b6ebec name="price" attributes=[#<Nokogiri::XML::Attr:0x2b6eb74 name="curr" value="GBP">] children=[#<Nokogiri::XML::Element:0x2b7256c name="buynow" children=[#<Nokogiri::XML::Text:0x2b72274 "47.00">]>, #<Nokogiri::XML::Element:0x2b71f7c name="delivery" children=[#<Nokogiri::XML::Text:0x2b71d10 "3.99">]>]>, #<Nokogiri::XML::Element:0x2b717d4 name="cat" children=[#<Nokogiri::XML::Element:0x2b7152c name="awCatId" children=[#<Nokogiri::XML::Text:0x2b7125c "579">]>, #<Nokogiri::XML::Element:0x2b70ff0 name="awCat" children=[#<Nokogiri::XML::Text:0x2b70d84 "Video Games">]>, #<Nokogiri::XML::Element:0x2b70a64 name="mCat" children=[#<Nokogiri::XML::Text:0x2b707bc "Main Menu|Electricals|Gaming &amp; Consoles|Video Games">]>]>, #<Nokogiri::XML::Element:0x2b742cc name="brand" children=[#<Nokogiri::XML::Element:0x2b73fd4 name="awBrandId" children=[#<Nokogiri::XML::Text:0x2b73d54 "427">]>, #<Nokogiri::XML::Element:0x2b73a84 name="brandName" children=[#<Nokogiri::XML::Text:0x2b737c8 "Xbox 360">]>]>]>

然后我尝试了(返回 nil):

result = file.xpath("//prod[contains(.,\"Platform: PS3\") and name = \Tour De France 2013\"]")[0]
#=> nil

我很难访问“名称”——我认为这可能是因为它嵌套很深。但我不知道如何引用它。

最佳答案

您可以使用以下 xpath 检查 name 和 desc 节点中包含特定文本的 prod 节点:

'//prod[text/name="Tour De France 2013" and text/desc[contains(text(), "Platform: XBOX 360")]]'

例如:

file.at_xpath('//prod[text/name="Tour De France 2013" and text/desc[contains(text(), "Platform: XBOX 360")]]')['id']
#=> "743854322"

关于ruby - Xpath Nokogiri嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18191521/

相关文章:

ruby - 使用 Nokogiri 创建非自闭空标签

ruby - MRI 内部结构 : detailed explanation of rb_id2str

ruby - block 中的变量范围

ruby-on-rails - 带句点(或句号)的 Rails 查询字符串。

xml - 获取没有特定祖先 xml xpath 的节点

xml - 数据库 XPath 查询

xml - 为什么 XPath 不适用于 xmlns 属性

ruby - 如何使用 Nokogiri 获取 div 中的所有内容?

xml - 如何获取节点的水平深度?

ruby - 无法通过 bundler 安装我自己的 gem,但可以使用 ruby​​gems 安装