Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is with your XPath selector. When you write:</p> <pre><code>i.xpath('//xmlns:AltitudeMeters') </code></pre> <p>you start at the root of the document and find every <code>&lt;AltitudeMeters&gt;</code> element at any level, not just children of the track that you are currently looking at. The minimum change you can make is to alter your XPath selector to be <code>.//xmlns:AltitudeMeters</code> (note the leading period):</p> <pre><code>doc = Nokogiri::XML(xml) tracks = doc.xpath( '//xmlns:Track' ) # You don't need to specify the namespace node_values = tracks.map do |track| { 'AltitudeMeters' =&gt; track.xpath('.//xmlns:AltitudeMeters').text } end p node_values #=&gt; {"AltitudeMeters"=&gt;"298.6267090"}, {"AltitudeMeters"=&gt;"498.6267090"}] </code></pre> <p>Additionally, if there is only one <code>Trackpoint</code> per track, I would use <code>at_xpath</code> instead, which returns the first matching element. Indeed, unless your schema is volatile, I would also specify exactly where to find the Altitude I wanted:</p> <pre><code>node_values = tracks.map do |track| { 'AltitudeMeters' =&gt; track.at_xpath('./xmlns:Trackpoint/xmlns:AltitudeMeters').text } end </code></pre> <p>Finally, since you seem to be working with a document with a single namespace, note that you can ask Nokogiri to drop all namespaces to make your life simpler:</p> <pre><code>doc = Nokogiri::XML(xml) doc.remove_namespaces! node_values = doc.xpath( '//Track/Trackpoint' ).map do |track| { 'Altitude' =&gt; track.at_xpath('./AltitudeMeters').text.to_f, 'Latitude' =&gt; track.at_xpath('./Position/LatitudeDegrees').text.to_f, 'Longitude' =&gt; track.at_xpath('./Position/LongitudeDegrees').text.to_f } end require 'pp' pp node_values #=&gt; [{"Altitude"=&gt;298.626709, "Latitude"=&gt;52.4864997, "Longitude"=&gt;13.3531452}, #=&gt; {"Altitude"=&gt;498.626709, "Latitude"=&gt;52.4864997, "Longitude"=&gt;13.3531452}] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload