gpt4 book ai didi

ruby-on-rails - 在 Nokogiri 中从 XML 中选择值 'doubled up'

转载 作者:数据小太阳 更新时间:2023-10-29 08:53:49 24 4
gpt4 key购买 nike

<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。但我不知道解决办法。

最佳答案

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

关于ruby-on-rails - 在 Nokogiri 中从 XML 中选择值 'doubled up',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com