开发者

XML + XPATH: Any way to work with a default Namespace?

开发者 https://www.devze.com 2023-01-10 15:20 出处:网络
I have a XML SOAP result: <?xml version=\"1.0\" encoding=\"utf-8\"?> <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"

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">
  <soap:Body>
    <CreateCIInStockResponse xmlns="http://somenamespace.com/">
      <CreateCIInStockResult>
        &开发者_StackOverflow中文版lt;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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号