Ruby 基于Nokogiri中的其他属性获取某些属性?
下面是我正在使用的XML:Ruby 基于Nokogiri中的其他属性获取某些属性?,ruby,xml,nokogiri,Ruby,Xml,Nokogiri,下面是我正在使用的XML: <order xmlns="http://example.com/schemas/1.0"> <link type="application/xml" rel="http://example.com/rel/self" href="https://example.com/orders/1631"/> <link type="application/xml" rel="http://example.com/rel/order/his
<order xmlns="http://example.com/schemas/1.0">
<link type="application/xml" rel="http://example.com/rel/self" href="https://example.com/orders/1631"/>
<link type="application/xml" rel="http://example.com/rel/order/history" href="http://example.com/orders/1631/history"/>
<link type="application/xml" rel="http://example.com/rel/order/transition/release" href="https://example.com/orders/1631/release"/>
<link type="application/xml" rel="http://example.com/rel/order/transition/cancel" href="https://example.com/orders/1631/cancel"/>
<state>hold</state>
<order-number>123-456-789</order-number>
<survey-title>Testing</survey-title>
<survey-url>http://example.com/s/123456</survey-url>
<number-of-questions>6</number-of-questions>
<number-of-completes>100</number-of-completes>
<target-group>
<country>
<id>US</id>
<name>United States</name>
</country>
<min-age>15</min-age>
</target-group>
<quote>319.00</quote>
<currency>USD</currency>
</order>
持有
123-456-789
测试
http://example.com/s/123456
6.
100
美国
美国
15
319
美元
我需要做的是从链接中获取href
属性,该链接的rel
为http://example.com/rel/order/transition/release
那么,我如何使用Nokogiri做到这一点呢?简易豌豆:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<order xmlns="http://example.com/schemas/1.0">
<link type="application/xml" rel="http://example.com/rel/self" href="https://example.com/orders/1631"/>
<link type="application/xml" rel="http://example.com/rel/order/history" href="http://example.com/orders/1631/history"/>
<link type="application/xml" rel="http://example.com/rel/order/transition/release" href="https://example.com/orders/1631/release"/>
<link type="application/xml" rel="http://example.com/rel/order/transition/cancel" href="https://example.com/orders/1631/cancel"/>
<state>hold</state>
<order-number>123-456-789</order-number>
<survey-title>Testing</survey-title>
<survey-url>http://example.com/s/123456</survey-url>
<number-of-questions>6</number-of-questions>
<number-of-completes>100</number-of-completes>
<target-group>
<country>
<id>US</id>
<name>United States</name>
</country>
<min-age>15</min-age>
</target-group>
<quote>319.00</quote>
<currency>USD</currency>
</order>
EOT
href = doc.at('link[rel="http://example.com/rel/order/transition/release"]')['href']
=> "https://example.com/orders/1631/release"
需要“nokogiri”
doc=Nokogiri::XML(这是一行代码:
@doc.xpath('//xmlns:link[@rel = "http://example.com/rel/order/transition/release"]').attr('href')
小心。Nokogiri的xpath
返回一个节点集,它实际上是一个数组。如果有多个节点与搜索匹配,则从数组中获取attr
将不会给出您期望的结果。例如,删除rel
的字符串匹配,只查找@rel
。您将得到找到的第一个结果。它是最好在
搜索中使用,或者使用数组索引指定所需节点集的哪个元素。正确。我假设提供的xml是规范的。对于一般解决方案,您的是要参考的解决方案。您需要向我们展示演示问题所需的最少代码。请参阅“”。这有助于我们在您的代码上下文中工作,而不是创建一个不相关的代码段,您必须尝试将其强制添加到您的代码中。此外,如果没有代码示例,您似乎没有尝试并希望我们为您编写代码,这一点也不酷。