我希望为我的XML查询函数编写一个XPath查询。我遇到的一个问题主要是因为我之前缺乏任何关于XML的知识,而且我已经被迫加入了一些问题。
但是,假设我有一个带有以下XML输出的文本框,我如何从<State>
的<ShipmentOrigin>
中提取信息?请注意,还有一些<State>
的其他实例,所以对<State>
进行全面搜索是行不通的,它必须专注于<State>
中的<ShipmentOrigin>
。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<TrackViaPronumberAdvancedResponse xmlns="https://webservices.rrts.com/tracking/">
<TrackViaPronumberAdvancedResult>
<ShipmentOrigin>
<Name>EXAMPLE</Name>
<Address1>EXAMPLE</Address1>
<Address2>EXAMPLE</Address2>
<City>EXAMPLE</City>
<State>EXAMPLE</State>
<PostalCode>EXAMPLE</PostalCode>
</ShipmentOrigin>
<ShipmentDestination>
<Name>EXAMPLE</Name>
<Address1>EXAMPLE</Address1>
<Address2/>
<City>EXAMPLE</City>
<State>EXAMPLE</State>
<PostalCode>EXAMPLE</PostalCode>
</ShipmentDestination>
<Details>
<Pronumber>EXAMPLE</Pronumber>
<PickupNumber/>
<CustomerNumber>EXAMPLE</CustomerNumber>
<BOLNumber/>
<PONumber>EXAMPLE</PONumber>
<Pieces>5</Pieces>
<Weight>214</Weight>
<AppointmentDate>9/20/2017</AppointmentDate>
<EstimatedDelivery>NA</EstimatedDelivery>
<DeliveredDate>9/20/2017</DeliveredDate>
<BillToNumber>5001868</BillToNumber>
<AppointmentTime>10:00 AM</AppointmentTime>
<ProjectedDeliveryDate>2017-09-25T00:00:00</ProjectedDeliveryDate>
<DeliveredTime/>
<HAWB/>
</Details>
<OriginTerminal>
<TerminalName>San Francisco</TerminalName>
<TerminalTollFreePhone>(877) 220-4582</TerminalTollFreePhone>
</OriginTerminal>
<Comments>
<ShipmentComment>
<CommentDate>09/14</CommentDate>
<ActivityCode>PU</ActivityCode>
<Comment>Shipment was picked up</Comment>
<StatusTime>12:03 AM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/25</CommentDate>
<ActivityCode>DUD</ActivityCode>
<Comment>Projected Delivery Date of 9/25/2017</Comment>
<StatusTime>12:03 AM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/16</CommentDate>
<ActivityCode>CLO</ActivityCode>
<Comment>Trailer Closed - ready for dispatch</Comment>
<StatusTime>2:00 AM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/17</CommentDate>
<ActivityCode>DSP</ActivityCode>
<Comment>Trailer dispatched to terminal</Comment>
<StatusTime>12:00 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/18</CommentDate>
<ActivityCode>ENR</ActivityCode>
<Comment>Trailer enroute: BRYAN, WY</Comment>
<StatusTime>3:02 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/19</CommentDate>
<ActivityCode>INV</ActivityCode>
<Comment>Invoice has been emailed</Comment>
<StatusTime>6:03 AM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/19</CommentDate>
<ActivityCode>ARX</ActivityCode>
<Comment>Trailer arrived at terminal in DENVER, CO</Comment>
<StatusTime>12:00 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/19</CommentDate>
<ActivityCode>UNL</ActivityCode>
<Comment>Trailer unloaded at terminal</Comment>
<StatusTime>12:01 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/19</CommentDate>
<ActivityCode>TRF</ActivityCode>
<Comment>Released to Delivery</Comment>
<StatusTime>5:00 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/20</CommentDate>
<ActivityCode>OFD</ActivityCode>
<Comment>Shipment out for delivery</Comment>
<StatusTime>12:00 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/20</CommentDate>
<ActivityCode>APT</ActivityCode>
<Comment>Appointment set for delivery to consignee on 9/20/2017</Comment>
<StatusTime>1:03 PM</StatusTime>
</ShipmentComment>
<ShipmentComment>
<CommentDate>09/20</CommentDate>
<ActivityCode>DED</ActivityCode>
<Comment>Shipment delivered to consignee</Comment>
<StatusTime>4:05 PM</StatusTime>
</ShipmentComment>
</Comments>
<BOLReceived>true</BOLReceived>
<PODReceived>true</PODReceived>
<InspectionAvailable>false</InspectionAvailable>
</TrackViaPronumberAdvancedResult>
</TrackViaPronumberAdvancedResponse>
</soap:Body>
</soap:Envelope>
答案取决于处理XPath的内容以及是否可以将命名空间绑定到前缀。
如果你可以将https://webservices.rrts.com/tracking/
命名空间绑定到前缀(例如tr
),你可以做这样的事情......
//tr:ShipmentOrigin/tr:State
如果您无法将命名空间绑定到前缀,则可以使用local-name()
...
//*[local-name()='ShipmentOrigin']/*[local-name()='State']
你可以/也应该使用namespace-uri()
来确保你完全匹配你想要匹配的东西......
//*[namespace-uri()='https://webservices.rrts.com/tracking/' and local-name()='ShipmentOrigin']/*[namespace-uri()='https://webservices.rrts.com/tracking/' and local-name()='State']