views:

27

answers:

1

I have a XML SOAP result:

<?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"&gt;
  <soap:Body>
    <CreateCIInStockResponse xmlns="http://somenamespace.com/"&gt;
      <CreateCIInStockResult>
        <Status>OK</Status>
        <Data>SOMERESULT</Data>
      </CreateCIInStockResult>
    </CreateCIInStockResponse>
  </soap:Body>
</soap:Envelope>

As you can see, the namespace defined in CreateCIInStockResponse uses a default namespace - no prefix defined.

I can get both Status and Data if we use

/soap:Envelope/soap:Body/node()/node()/node()/text()

Am I right, that there is no way - using XPath - to access the content of "Data" directly?

My problem is, that I can neither modify the call to the webservice nor modify the result coming back from the webservice. All I can do is use a XPath to get my data out.

Any suggestions?

+3  A: 

Assign http://somenamespace.com/ to a namespace prefix, say "def", and use that in your XPath expression:

/soap:Envelope/soap:Body/def:CreateCIInStockResponse/def:Data

How to assign the prefix will vary depending on your XPath processor.

Updated: Alternative approach if assigning a prefix is not an option:

/soap:Envelope/soap:Body/*[local-name()='CreateCIInStockResponse']/*[local-name()='Data']

To make absolutely sure that you're accessing the elements you expect, you might add namespace-uri() = 'http://somenamespace.com/' as well.

mwittrock
My problem is, that I can't access the program code of the application using the XPath. I just have an input box where I can enter my XPath string. But your answer is exactly what I was afraid to get as an answer... *sigh*
BlaM
@mwittrock: Good answer, and good edit! +1
Alejandro
Great, that works beautifully!
BlaM
Glad to hear it.
mwittrock