使用 XPath 获取具有默认命名空间(无命名空间前缀)的元素 [英] Getting elements with default namespace (no namespace prefix) using XPath
本文介绍了使用 XPath 获取具有默认命名空间(无命名空间前缀)的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这个 SOAP XML 文件中,如何使用 XPath 查询获取 7
?
In this SOAP XML file, how can I get the 7
on a using a XPath query?
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<HelloWorldResponse xmlns="http://tempuri.org/">
<HelloWorldResult>7</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
此 XPath 查询不起作用 //*[name () ='soap:Body']
.
This XPath query is not working //*[name () ='soap:Body']
.
推荐答案
如果你有一个命名空间前缀集,你可以使用它,比如:
If you have a namespace prefix set, you could use it, like:
//soap:Body
但是由于您尝试获取的节点使用默认命名空间,没有前缀,使用纯 XPath,您只能通过 local-name()
和 namespace-uri()
属性.示例:
But since the nodes you are trying to get use a default namespace, without a prefix, using plain XPath, you can only acesss them by the local-name()
and namespace-uri()
attributes. Examples:
//*[local-name()="HelloWorldResult"]/text()
或者:
//*[local-name()="HelloWorldResult" and namespace-uri()='http://tempuri.org/']/text()
或者:
//*[local-name()="HelloWorldResponse" and namespace-uri()='http://tempuri.org/']/*[local-name()="HelloWorldResult"]/text()
对于您的 xml,它们都会给出相同的结果,文本 7
.
To your xml, they will all give the same result, the text 7
.
这篇关于使用 XPath 获取具有默认命名空间(无命名空间前缀)的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文