这里是我的工作的XML:根据Nokogiri中的其他属性获取某些属性?
<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>
我需要做的是让href
属性,从具有link
的http://example.com/rel/order/transition/release
所以一个rel
,我该怎么办,使用引入nokogiri?
要小心。 Nokogiri的'xpath'返回一个NodeSet,它实际上是一个数组。如果多个节点与搜索匹配,从数组中获取'attr'将不会为您提供您期望的结果。例如,删除'rel'的字符串匹配,只查找'@ rel'。你会得到第一个结果。最好使用'at'搜索,或者使用数组索引来指定您想要的NodeSet的哪个元素。 –
正确。我认为提供的XML是规范的。对于一个通用的解决方案,你的是一个参考。 –