开发者

Values 'doubled up' selecting out of XML in Nokogiri

<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
  <Activities>
    <Activity Sport="Running">
      <Id>2011-04-29T15:29:42Z</Id>
      <Lap StartTime="2011-04-29T15:29:42Z">
        <TriggerMethod>Manual</TriggerMethod>
        <Track>
          <Trackpoint>
            <AltitudeMeters>298.6267090</AltitudeMeters>
            <Position>
              <LatitudeDegrees>52.4864997</LatitudeDegrees>
              <LongitudeDegrees>13.3531452</LongitudeDegrees>
            </Position>
          </Trackpoint>
        </Track>
      </Lap>
      <Lap StartTime="2011-04-29T15:29:42Z">
        <TriggerMethod>Manual</TriggerMethod>
        <Track>
          <Trackpoint>
            <AltitudeMeters>498.6267090</AltitudeMeters>
            <Position>
              <LatitudeDegrees>52.4864997</LatitudeDegrees>
              <LongitudeDegrees>13.3531452</LongitudeDegrees>
            </Position>
          </Trackpoint>
         </Track>
      </Lap>
    </Activity>
  </Activities>
</TrainingCenterDatabase>
doc = Nokogiri::XML(xml)
node_values = doc.xpath('//xmlns:Track', 'xmlns' => 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2').map do |i|     
  {'AltitudeMeters' => i.xpath('//xmlns:AltitudeMeters').text}
end
nl.debug(node_values)

I always get double entries in my result:

[{"AltitudeMeters"=>"298.6267090498.6267090"},
 {"AltitudeMeters"=>"298.6267090498.6267090"}]

I want something like this:

[{"AltitudeMeters"=>"298.6267090"},
 {"AltitudeMeters"=>"498.6267090"}]

The problem could be the xmlns. But I 开发者_JAVA百科don't know a solution.


doc = Nokogiri::XML(xml)
node_values = doc.xpath('//xmlns:Track//xmlns:AltitudeMeters', 'xmlns' => 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2').map do |i|     
    {'AltitudeMeters' => i.text}
end
p node_values
# => [{"AltitudeMeters"=>"298.6267090"}, {"AltitudeMeters"=>"498.6267090"}]


Your problem is with your XPath selector. When you write:

i.xpath('//xmlns:AltitudeMeters')

you start at the root of the document and find every <AltitudeMeters> 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 .//xmlns:AltitudeMeters (note the leading period):

doc = Nokogiri::XML(xml)
tracks = doc.xpath( '//xmlns:Track' ) # You don't need to specify the namespace
node_values = tracks.map do |track|
  { 'AltitudeMeters' => track.xpath('.//xmlns:AltitudeMeters').text }
end
p node_values
#=> {"AltitudeMeters"=>"298.6267090"}, {"AltitudeMeters"=>"498.6267090"}]

Additionally, if there is only one Trackpoint per track, I would use at_xpath 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:

node_values = tracks.map do |track|
  { 'AltitudeMeters' =>
    track.at_xpath('./xmlns:Trackpoint/xmlns:AltitudeMeters').text }
end

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:

doc = Nokogiri::XML(xml)
doc.remove_namespaces!
node_values = doc.xpath( '//Track/Trackpoint' ).map do |track|
  {
    'Altitude'  => track.at_xpath('./AltitudeMeters').text.to_f,
    'Latitude'  => track.at_xpath('./Position/LatitudeDegrees').text.to_f,
    'Longitude' => track.at_xpath('./Position/LongitudeDegrees').text.to_f
  }
end

require 'pp'
pp node_values
#=> [{"Altitude"=>298.626709, "Latitude"=>52.4864997, "Longitude"=>13.3531452},
#=>  {"Altitude"=>498.626709, "Latitude"=>52.4864997, "Longitude"=>13.3531452}]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜