如何使用Nokogiri从xml标记内部提取值?

时间:2014-03-06 01:17:26

标签: xml nokogiri

如何从ID下方提取TrackInfo

<?xml version="1.0" encoding="UTF-8"?> <TrackResponse> <TrackInfo ID="9400110200883105817710"> <TrackSummary>Your item departed our ATLANTA, GA 30320 sort facility on February 25, 2014. Information, if available, is updated periodically throughout the day. Please check again later.</TrackSummary> <TrackDetail>Processed through USPS Sort Facility, February 25, 2014, 11:54 am, ATLANTA, GA 30320</TrackDetail> <TrackDetail>Depart USPS Sort Facility, February 22, 2014, ORLANDO, FL 32824</TrackDetail> <TrackDetail>Processed at USPS Origin Sort Facility, February 21, 2014, 4:39 am, ORLANDO, FL 32824</TrackDetail> <TrackDetail>Accepted at USPS Origin Sort Facility, February 21, 2014, 3:24 am, ORLANDO, FL 32819</TrackDetail> <TrackDetail>Electronic Shipping Info Received, February 15, 2014</TrackDetail> </TrackInfo> </TrackResponse>

当我尝试@xml.xpath('//TrackInfo')时,我得到了整个身体而不是身份证。

由于

1 个答案:

答案 0 :(得分:2)

您可以使用@选择属性节点:

@xml.xpath('//TrackInfo/@ID')

(您也可以直接指定attribute轴 - //TrackInfo/attribute::ID - 但通常会使用缩写@语法。)