XPath:选择文本节点 [英] XPath: select text node
问题描述
具有以下 XML:
<node>Text1<subnode/>text2</node>
如何通过 XPath 选择第一个或第二个文本节点?
How do I select either the first or the second text node via XPath?
像这样:
/node/text()[2]
当然不行,因为它是节点内每个文本的合并结果.
of course doesn't work because it's the merged result of every text inside the node.
推荐答案
具有以下 XML:
Having the following XML:
<node>Text1<subnode/>text2</node>
我如何选择第一个或通过 XPath 的第二个文本节点?
How do I select either the first or the second text node via XPath?
使用:
/node/text()
这将选择 XML 文档顶部元素(名为节点")的所有文本节点子节点.
This selects all text-node children of the top element (named "node") of the XML document.
/node/text()[1]
这将选择 XML 文档顶部元素(名为节点")的第一个文本节点子节点.
This selects the first text-node child of the top element (named "node") of the XML document.
/node/text()[2]
这将选择 XML 文档顶部元素(名为节点")的第二个文本节点子节点.
This selects the second text-node child of the top element (named "node") of the XML document.
/node/text()[someInteger]
这将选择 XML 文档顶部元素(名为节点")的 someInteger-th 文本节点子节点.它等效于以下 XPath 表达式:
This selects the someInteger-th text-node child of the top element (named "node") of the XML document. It is equivalent to the following XPath expression:
/node/text()[position() = someInteger]
这篇关于XPath:选择文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!