2

There are lots of answers with no namespaces involved but I'm having trouble with the namespace.

Here's the xml doc:

<GetServiceRequestDetailsResponse xmlns="http://lcc.catscrm">
<GetServiceRequestDetailsResult xmlns:a="http://schemas.datacontract.org/2004/07/LCC.CATS.CRM.WCF.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:AltRefs>
        <a:AltRefModel>
            <a:Name>UTRN</a:Name>
            <a:TypeId>7577</a:TypeId>
            <a:Value>9024930</a:Value>
        </a:AltRefModel>
    </a:AltRefs>
    <a:Customer>
        <a:Address>
            <a:Address1>5</a:Address1>
            <a:Address2>Random</a:Address2>
            <a:County>West Yorks</a:County>
            <a:NumberName>5</a:NumberName>
            <a:Postcode>L1</a:Postcode>
            <a:TownCity>London</a:TownCity>
            <a:UPRN i:nil="true"/>
        </a:Address>
        <a:AltRefs/>
        <a:ContactTransports>
            <a:ContactTransport>
                <a:Name>MobileNumber</a:Name>
                <a:TypeId>3</a:TypeId>
                <a:Value>08965</a:Value>
            </a:ContactTransport>
            <a:ContactTransport>
                <a:Name>Email</a:Name>
                <a:TypeId>1</a:TypeId>
                <a:Value>a@b.com</a:Value>
            </a:ContactTransport>
        </a:ContactTransports>
        <a:CustomerUPRN i:nil="true"/>
        <a:FirstName>Rob</a:FirstName>
        <a:Id i:nil="true"/>
        <a:LastName>Bowman</a:LastName>
    </a:Customer>
</GetServiceRequestDetailsResult>

I need to get the /ContractTransport/Value where the /ContractTransport/Name='Email'.

The following xpath gets me to ContactTransport /*[local-name()='GetServiceRequestDetailsResponse']/*[local-name()='GetServiceRequestDetailsResult']/*[local-name()='Customer']/*[local-name()='ContactTransports']/*[local-name()='ContactTransport']

If there were no namespaces then I guess I could suffix with [Name='Email]/*[local-name()='Value'] but this doesn't work now.

Rob Bowman
  • 7,632
  • 22
  • 93
  • 200

2 Answers2

3

You can add

/*[local-name()="Value" and preceding-sibling::*[local-name()="Name" and .="Email"]]

to your existed XPath to get required output

Andersson
  • 51,635
  • 17
  • 77
  • 129
2

This might not be the most elegant option, but here's one:

//*[local-name()='ContactTransport']/*[local-name()='Value' and ../*[local-name()='Name' and text()='Email']]

This will select all Value elements underneath a ContactTransport that have a sibling whose Name text is Email. Demo.

scrowler
  • 24,273
  • 9
  • 60
  • 92