Jesus Paradinas
Jesus Paradinas

Reputation: 187

How to get a child value with XPath

I have this xml

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <loginResponse xmlns="http://www.tedial.com/3rdparty/" 
xmlns:ns2="http://www.tedial.com/apiextension/">
         <session>1C7AE89A-73BF-01E9-9D3F-0010007FFF00</session>
      </loginResponse>
   </S:Body>
</S:Envelope>

I was trying so many combinations but I am unable to get the session value. Can you help me?

I tried //S:Envelope//S:Body//ns2:loginResponse//ns2:session with no luck

Upvotes: 0

Views: 222

Answers (3)

Jesus Paradinas
Jesus Paradinas

Reputation: 187

I got it working now. The problem was happening in SoapUI. For some reason Soap UI automatically uses namespaces for the default namespaces starting with ns1. In my case this Xpath expression worked fine: //S:Envelope//S:Body//ns1:loginResponse//ns1:session

Upvotes: 0

zx485
zx485

Reputation: 29022

You used a wrong namespace on the session element. The default namespace of loginResponse - xmlns="http://www.tedial.com/3rdparty/" - is inherited to the session element. You have to use the same namespace as with loginResponse which you - erroneously - assigned the ns2 namespace. So define a third namespace prefix for http://www.tedial.com/3rdparty/ - here I used third - and use that for loginResponse and session:

/S:Envelope/S:Body/third:loginResponse/third:session

Upvotes: 2

OldProgrammer
OldProgrammer

Reputation: 12169

Try this:

//S:Envelope//S:Body/loginResponse/session/text()

ns2 prefix not needed.

Upvotes: 0

Related Questions