价值“翻倍”在 Nokogiri 中选择 XML
<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。但我不知道解决办法。
<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 don't know a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题出在您的 XPath 选择器上。当您编写时:
您从文档的根部开始,找到任何级别的每个
元素,而不仅仅是您当前正在查看的轨道的子元素。您可以进行的最小更改是将 XPath 选择器更改为.//xmlns:AltitudeMeters
(注意前导句点):此外,如果每个跟踪点只有一个
Trackpoint
跟踪,我会使用at_xpath
代替,它返回第一个匹配元素。事实上,除非您的架构不稳定,否则我还会准确指定在哪里找到我想要的 Altitude:最后,由于您似乎正在使用具有单个命名空间的文档,请注意,您可以要求 Nokogiri 删除所有命名空间以使您的生活更简单:
Your problem is with your XPath selector. When you write:
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):Additionally, if there is only one
Trackpoint
per track, I would useat_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: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: