Here’s the XML I’m working with:
<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>
What I need to do is get the href attribute, from the link that has a rel of http://example.com/rel/order/transition/release
So, how can I do that using Nokogiri?
Easy-peasy:
This is using Nokogiri’s ability to use CSS accessors. Sometimes it’s easier (or the only way) to use XPath, but I prefer CSS because they tend to be more readable.
Nokogiri::Node.atcan take a CSS accessor or XPath, and will return the first node matching that pattern. If you need to iterate over all the matches, usesearchinstead, which returns aNodeSet, which you can treat as an array. Nokogiri also supportsat_xpathandat_cssalong withcssandxpathforatandsearchsymmetry.