How to get Nokogiri to resolve XPath references when parsing XML?

M

Matthias S.

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:

<element1>
<element2>
<location>
<longitude>...
<latitude>...
</location>
</element2>
<element3>
<location reference=""/>
</element3>
</element1>

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?
 
R

Robert Klemme

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:

<element1>
=A0<element2>
=A0<location>
=A0 <longitude>...
=A0 <latitude>...
=A0</location>
=A0</element2>
=A0<element3>
=A0<location reference=3D""/>
=A0</element3>
</element1>

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?

I don't think you can get Nokogiri (or any other XPath processor) to
handle this - at least I am unaware of any standard way to use
arbitrary attributes as references. IIRC there are specific
attributes defined for referencing other XML elements (by id?) but
even then I do not believe that you can create an XPath expression
that will return synthetic nodes.

Apart from that your reference attribute does not contain any
information which would allow for identification of the referred node.
If you cannot change the XML there is no other way as to iterate all
locations and remember the values of the last location which can then
be used on nodes which have a "reference" attribute.

If you can change the XML I suggest you read up on what w3c says about
referencing XML nodes from other nodes and create a solution using
that.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top