Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3804324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:30:01+00:00 2026-05-19T14:30:01+00:00

A lot has been written about Nokogiri in terms of reading XML using XPath.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:30:02+00:00Added an answer on May 19, 2026 at 2:30 pm

    You can collect location nodes that have actual values (non-reference nodes) and then collect all references separately, as the following snippet demonstrates:

    require 'nokogiri'
    
    xml = <<End
    <elements>
     <element>
      <location>
       <longitude>45</longitude>
       <latitude>-70</latitude>
      </location>
     </element>
     <element>
      <location reference="../../element/location"/>
     </element>
    </element>
    End
    
    doc = Nokogiri::XML(xml)
    
    #Collect all the explicit longitudes
    longitudes = doc.search('//location[not(@reference)]/longitude').map(&:text)
    
    #Follow references to longitudes
    doc.search('//location[@reference]').each do |location|
      reference = location.attribute('reference')
      longitudes << location.xpath("#{reference}/longitude").text
    end
    
    puts longitudes #=> ["45", "45"]
    

    You can build on this technique to extract whatever information you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know a lot has been written about Linq and it's inner workings. Inspired
We have a data driven ASP.NET website which has been written using the standard
A lot has been said about mutable / immutable objects in the data model.
There has been a lot of talk around iPad-Apps / Approval / Store-related Questions.
I know this has been discussed a lot of times but is there any
This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're
I've been give some lovely Java code that has a lot of things like
Currently I'm reading some documentation written by another and I'm confused. A lot of
I am maintaining an application that has classes (written in 2005) that extend javax.xml.messaging.JAXMServlet
I have been using Selenium a lot lately (testing an ExtJs app) and while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.