XML + XPATH: Any way to work with the default namespace?

I have an 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"> <soap:Body> <CreateCIInStockResponse xmlns="http://somenamespace.com/"> <CreateCIInStockResult> <Status>OK</Status> <Data>SOMERESULT</Data> </CreateCIInStockResult> </CreateCIInStockResponse> </soap:Body> </soap:Envelope> 

As you can see, the namespace defined in CreateCIInStockResponse uses the default namespace - no prefix is ​​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 directly access the contents of the "Data"?

My problem is that I can neither change the call to the web service, nor change the result returned from the web service. All I can do is use XPath to output my data.

Any suggestions?

+3
source share
1 answer

Assign http://somenamespace.com/ to the namespace prefix, say "def", and use it in the XPath expression:

/ soap: Envelope / soap: Body / Def: CreateCIInStockResponse / Def: Data

How to assign a 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 sure that you are accessing the items you expect, you can add namespace-uri () = ' http://somenamespace.com/ ' as well.

+8
source

All Articles