XML + XPATH:任何使用默认命名空间的方法? [英] XML + XPATH: Any way to work with a default Namespace?
问题描述
我有一个 XML SOAP 结果:
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>
<Status>OK</Status>
<Data>SOMERESULT</Data>
</CreateCIInStockResult>
</CreateCIInStockResponse>
</soap:Body>
</soap:Envelope>
如您所见,CreateCIInStockResponse 中定义的命名空间使用默认命名空间 - 未定义前缀.
As you can see, the namespace defined in CreateCIInStockResponse uses a default namespace - no prefix defined.
如果我们使用
/soap:Envelope/soap:Body/node()/node()/node()/text()
我是对的,没有办法 - 使用 XPath - 直接访问数据"的内容?
Am I right, that there is no way - using XPath - to access the content of "Data" directly?
我的问题是,我既不能修改对 webservice 的调用,也不能修改从 webservice 返回的结果.我所能做的就是使用 XPath 来获取我的数据.
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.
有什么建议吗?
推荐答案
Assign http://somenamespace.com/命名空间前缀,说def",并在您的 XPath 表达式中使用它:
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
/soap:Envelope/soap:Body/def:CreateCIInStockResponse/def:Data
如何分配前缀会因您的 XPath 处理器而异.
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']
为了绝对确保您正在访问您期望的元素,您可以添加 namespace-uri() = 'http://somenamespace.com/' 也是如此.
To make absolutely sure that you're accessing the elements you expect, you might add namespace-uri() = 'http://somenamespace.com/' as well.
这篇关于XML + XPATH:任何使用默认命名空间的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!