如何使用 XPath 在具有命名空间的 XML 文档中查找所有文本节点? [英] How do I find all text nodes in an XML document with a namespace using XPath?
问题描述
假设我有这个 XML:
Suppose I have this XML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="http://www.foo.com">
<foo:one>Alpha</foo:one>
<two foo:Dog="Yorkie">Bravo</two>
<three>foo:Delta</three>
</root>
如果我想使用 XPath 来查找所有在http://www.foo.com"命名空间中的元素(比如元素one"),我可以使用这个:
If I want to use XPath to find all elements that are in the "http://www.foo.com" namespace (such as the element "one"), I can use this:
//*[namespace-uri()='http://www.foo.com']
如果我想找到在该命名空间中具有属性的所有元素(例如元素two"),我可以使用这个:
If I want to find all elements that have an attribute in that namespace (such as the element "two"), I can use this:
//@*[namespace-uri()='http://www.foo.com']
如果我想查找在该命名空间中具有文本节点的所有元素(例如元素three"),我可以使用什么 XPath?
If I want to find all elements that have a text node in that namespace (such as the element "three"), what XPath can I use?
允许 XPath 1 或 2.谢谢.
XPath 1 or 2 allowed. Thanks.
推荐答案
文本节点上没有命名空间;您的 three
节点只包含文本 foo:Delta
,它在 XML 中没有特殊含义.您可以使用 starts-with
:
There is no such thing as a namespace on a text node; your three
node just contains the text foo:Delta
, which has no special meaning in XML. You can find nodes holding text starting with foo:
using starts-with
:
//*[starts-with(., "foo:")]
这篇关于如何使用 XPath 在具有命名空间的 XML 文档中查找所有文本节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!