【发布时间】:2017-04-26 08:49:45
【问题描述】:
使用 Nokogiri::XML 如何根据另一个属性检索一个属性的值?
XML 文件:
<RateReplyDetails>
<ServiceType>INT</ServiceType>
<Price>1.0</Price>
</RateReplyDetails>
<RateReplyDetails>
<ServiceType>LOCAL</ServiceType>
<Price>2.0</Price>
</RateReplyDetails>
我想检索 LOCAL ServiceType 的价格,即 2.0
我可以在没有任何条件的情况下取值:
rated_shipment.at('RateReplyDetails/Price').text
也许我可以这样做:
if rated_shipment.at('RateReplyDetails/ServiceType').text == "LOCAL"
rated_shipment.at('RateReplyDetails/Price').text
但是有什么优雅而干净的方式吗?
【问题讨论】:
标签: ruby-on-rails ruby nokogiri