Xpath 匹配表达式给出两个字符串值

Xpath matching expression gives two strings values

我有提取以下值的要求。

<cidx:ReferenceInformation ReferenceType="DeliveryNoteNumber">
                    <cidx:DocumentReference>
                        <cidx:DocumentIdentifier>5004330471</cidx:DocumentIdentifier>
                    </cidx:DocumentReference>
                </cidx:ReferenceInformation>

我正在使用下面的 Xpath 表达式并获得 2 个值匹配。

表达式

/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticeProperties']/*[local-name()='ReferenceInformation']/*[local-name()='DocumentReference']/*[local-name()='DocumentIdentifier']/text()

输出

 <?xml version="1.0" encoding="UTF-8"?>
<result>
5004330471
0803692106
</result>

我测试过在线工具中的Xpath [http://www.xpathtester.com/xpath][1]

下面是INPUT XML

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header/>
<SOAP:Body>
    <cidx:ShipNotice xmlns:cidx="urn:cidx:names:specification:ces:schema:all:4:0" Version="4.0">
        <cidx:Header>
            <cidx:ThisDocumentIdentifier>
                <cidx:DocumentIdentifier>0000001113658104</cidx:DocumentIdentifier>
            </cidx:ThisDocumentIdentifier>
            <cidx:ThisDocumentDateTime>
                <cidx:DateTime DateTimeQualifier="On">2015-01-22T20:15:35Z</cidx:DateTime>
            </cidx:ThisDocumentDateTime>
            <cidx:From>
                <cidx:PartnerInformation>
                    <cidx:PartnerName>MOS Company</cidx:PartnerName>
                    <cidx:PartnerIdentifier Agency="AGIIS-EBID">1234567890123</cidx:PartnerIdentifier>
                    <cidx:AddressInformation>
                        <cidx:AddressLine>N. Lindbergh</cidx:AddressLine>
                        <cidx:CityName>Columbia</cidx:CityName>
                        <cidx:StateOrProvince>MO</cidx:StateOrProvince>
                        <cidx:PostalCode>63190</cidx:PostalCode>
                        <cidx:PostalCountry>US</cidx:PostalCountry>
                    </cidx:AddressInformation>
                </cidx:PartnerInformation>
            </cidx:From>
            <cidx:To>
                <cidx:PartnerInformation>
                    <cidx:PartnerName> DIV BOWLNG GR VERA</cidx:PartnerName>
                    <cidx:PartnerIdentifier Agency="AssignedByPapiNet">0001664057</cidx:PartnerIdentifier>
                    <cidx:ContactInformation>
                        <cidx:ContactName>2015</cidx:ContactName>
                        <cidx:ContactDescription>SeedYear</cidx:ContactDescription>
                    </cidx:ContactInformation>
                    <cidx:ContactInformation>
                        <cidx:ContactName>1024122440000</cidx:ContactName>
                        <cidx:ContactDescription>AGIIS-EBID</cidx:ContactDescription>
                    </cidx:ContactInformation>
                    <cidx:AddressInformation>
                        <cidx:AddressLine>17410 PIKE 291</cidx:AddressLine>
                        <cidx:CityName>BOWLING GREEN</cidx:CityName>
                        <cidx:StateOrProvince>MO</cidx:StateOrProvince>
                        <cidx:PostalCode>633343045</cidx:PostalCode>
                        <cidx:PostalCountry>US</cidx:PostalCountry>
                    </cidx:AddressInformation>
                </cidx:PartnerInformation>
            </cidx:To>
        </cidx:Header>
        <cidx:ShipNoticeBody>
            <cidx:ShipNoticeProperties>
                <cidx:ShipmentIdentification>
                    <cidx:DocumentReference>
                        <cidx:DocumentIdentifier>0803692106</cidx:DocumentIdentifier>
                    </cidx:DocumentReference>
                </cidx:ShipmentIdentification>
                <cidx:ShipDate>
                    <cidx:DateTime DateTimeQualifier="On">2015-01-22T00:00:00Z</cidx:DateTime>
                </cidx:ShipDate>
                <cidx:PurchaseOrderInformation>
                    <cidx:DocumentReference>
                        <cidx:DocumentIdentifier>PO8956234</cidx:DocumentIdentifier>
                        <cidx:ReferenceItem>000530</cidx:ReferenceItem>
                    </cidx:DocumentReference>
                </cidx:PurchaseOrderInformation>
                <cidx:TransportMethodCode Domain="UN-Rec-19">3</cidx:TransportMethodCode>
                <cidx:ReferenceInformation ReferenceType="DeliveryNoteNumber">
                    <cidx:DocumentReference>
                        <cidx:DocumentIdentifier>5004330471</cidx:DocumentIdentifier>
                    </cidx:DocumentReference>
                </cidx:ReferenceInformation>
                <cidx:ReferenceInformation ReferenceType="BillOfLadingNumber">
                    <cidx:DocumentReference>
                        <cidx:DocumentIdentifier>0803692106</cidx:DocumentIdentifier>
                    </cidx:DocumentReference>
                </cidx:ReferenceInformation>
                <cidx:ShipNoticeDate>
                    <cidx:DateTime DateTimeQualifier="On">2015-01-22T20:15:35Z</cidx:DateTime>
                </cidx:ShipNoticeDate>
            </cidx:ShipNoticeProperties>
        </cidx:ShipNoticeBody>
    </cidx:ShipNotice>
</SOAP:Body>
</SOAP:Envelope>

谁能告诉我如何只获得这个值?

<cidx:DocumentIdentifier>5004330471</cidx:DocumentIdentifier>

看来您需要测试 cidx:ReferenceInformation 元素中的 ReferenceType 属性:

/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ShipNotice']/*[local-name()='ShipNoticeBody']/*[local-name()='ShipNoticeProperties']/*[local-name()='ReferenceInformation' and @ReferenceType='DeliveryNoteNumber']/*[local-name()='DocumentReference']/*[local-name()='DocumentIdentifier']

注意:如果您可以声明名称空间并在路径中使用前缀,则可以清理很多 xpath。

如果您使用的是 XPath 2.0,您也可以使用 * 作为前缀来清理它...

/*:Envelope/*:Body/*:ShipNotice/*:ShipNoticeBody/*:ShipNoticeProperties/*:ReferenceInformation[@ReferenceType='DeliveryNoteNumber']/*:DocumentReference/*:DocumentIdentifier