XML查询命名空间&节点

时间:2017-10-02 17:03:50

标签: xml web-services xpath soap

我希望为我的XML查询函数编写一个XPath查询。我遇到的一个问题主要是因为我之前对XML的知识缺乏了解,而且我已经被迫加入了它。

但是,假设我有一个带有以下XML输出的文本框,我如何从<State>的{​​{1}}中提取信息?请注意,<ShipmentOrigin>还有其他几个实例,因此对<State>进行全面搜索是行不通的,必须专注于<State> <State><ShipmentOrigin> 1}}。

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

1 个答案:

答案 0 :(得分:1)

答案取决于处理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']