Question

How can I extract the ID from TrackInfo below?

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

When I try @xml.xpath('//TrackInfo') I'm getting the entire body rather then just the ID.

Thanks

Was it helpful?

Solution

You can use @ to select an attribute node:

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

(You could also specify the attribute axis directly – //TrackInfo/attribute::ID – but the abbreviated @ syntax is normally used.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top