On Sat, Jan 29, 2011 at 1:00 PM, Matthias S. <mstumpp / googlemail.com> wrote=
:
> 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/