Michal Krasny
Michal Krasny

Reputation: 5916

What is the XPath of an element in a different, default namespace?

I deal with this SOAP response:

<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>
      <StartProcessFileResponse xmlns="http://www.abbyy.com/RecognitionServer4_xml/RecognitionServer4.xml">
         <StartProcessFileResult>{B4815D0C-91F9-4BD3-BF2F-3A70E935AA7B}</StartProcessFileResult>
      </StartProcessFileResponse>
   </soap:Body>
</soap:Envelope>

I need to get XPath to the the StartProcessFileResult element, but when I try this XPath in an online XPath validator, I get no match:

/soap:Envelope/soap:Body/StartProcessFileResponse/StartProcessFileResult

I tried a smaller piece of XPath and this XPath /soap:Envelope/soap:Body returns the <body> element, but when I go further and try to access StartProcessFileResponse with this XPath /soap:Envelope/soap:Body/StartProcessFileResponse, I get no match.

What am I doing wrong?

Upvotes: 3

Views: 449

Answers (1)

kjhughes
kjhughes

Reputation: 111621

StartProcessFileResult is in the http://www.abbyy.com/RecognitionServer4_xml/RecognitionServer4.xml default namespace.

Using the namespace binding facilities of your XPath library, bind a namespace prefix, say rs to http://www.abbyy.com/RecognitionServer4_xml/RecognitionServer4.xml.

Then the following XPath will select the StartProcessFileResult as requested:

/soap:Envelope/soap:Body/rs:StartProcessFileResponse/rs:StartProcessFileResult

If you cannot bind a namespace prefix, you can use the local-name() function instead,

/soap:Envelope/soap:Body/*[local-name()='StartProcessFileResponse']/*[local-name()='StartProcessFileResult']

but the preferred way is to make and use the namespace prefix binding.

Upvotes: 3

Related Questions