在Nokogiri中选择退出XML的价值“加倍”

时间:2011-06-26 16:36:45

标签: ruby-on-rails ruby nokogiri xml-namespaces

<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)

我的结果总是得到双重输入:

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

我想要这样的事情:

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

问题可能是xmlns。但我不知道解决方案。

2 个答案:

答案 0 :(得分:2)

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"}]

答案 1 :(得分:1)

您的问题在于您的XPath选择器。当你写:

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

从文档的根开始,找到任何级别的每个<AltitudeMeters>元素,而不仅仅是您当前正在查看的轨道的子级。您可以进行的最小更改是将XPath选择器更改为.//xmlns:AltitudeMeters(请注意前导时段):

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"}]

此外,如果每个曲目只有一个Trackpoint,我会使用at_xpath代替,它会返回第一个匹配元素。实际上,除非您的架构是易变的,否则我还会指定在哪里找到我想要的Altitude:

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

最后,由于您似乎正在处理具有单个命名空间的文档,请注意您可以要求Nokogiri删除所有命名空间以使您的生活更简单:

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}]