A lot has been written about Nokogiri in terms of reading XML using XPath. However, what’s about using Nokogiri with XML containing XPath references.
In the example, the xml contains a XPath reference:
<elements>
<element>
<location>
<longitude>...
<latitude>...
</location>
</element>
<element>
<location reference="../../element/location"/>
</element>
</elements>
Since both location elements are equal, only the first element is described in detail. The second just references the first.
Using Nokogiri, xml.xpath(‘//location’) returns two node instances as expected. The first node contains all child nodes. The second only a reference as an attribute of the second node instance.
Ok, assuming I want to request all longitude values, I would do xml.xpath(‘//location/longitude’). This returns only one node instance. However, since there are actually two elements of type “longitude”, I expected to receive two node instances, expecting Nokogiri to resolve XPath references…
How do I achieve this with Nokogiri?
You can collect location nodes that have actual values (non-reference nodes) and then collect all references separately, as the following snippet demonstrates:
You can build on this technique to extract whatever information you want.