只在节点中直接选择文本,而不是在子节点中 [英] Only select text directly in node, not in child nodes
问题描述
如何在不选择子节点中的文本的情况下检索节点中的文本?
<div class="title">编辑说明</div><div class="changed">上次更新:</div><br class="clear">Lorem ipsum dolor 坐 amet.
换句话说,我想要Lorem ipsum dolor sat amet.
而不是编辑器描述最后更新:Lorem ipsum dolor sat amet.
在提供的 XML 文档中:
<div class="title">编辑说明</div><div class="changed">上次更新:</div><br class="clear">Lorem ipsum dolor 坐 amet.
顶部元素 /div
有 4 个子节点,它们是文本节点.这四个 text-node
子节点中的前三个是 whitespace-only
.这 4 个 text-node
子节点中的最后一个是需要的.
使用:
/div/text()[last()]
这与不同:
/div/text()
后者可能(取决于 whitespace-only
节点是否被 XML 解析器保留)选择所有 4 个文本节点,但您只需要它们中的最后一个.
另一种选择是(当您不确切知道您想要哪个 text-node
时):
/div/text()[normalize-space()]
这将选择 /div
的所有 text-node-children
不是 whitespace-only
文本节点.
How does one retrieve the text in a node without selecting the text in the children?
<div id="comment">
<div class="title">Editor's Description</div>
<div class="changed">Last updated: </div>
<br class="clear">
Lorem ipsum dolor sit amet.
</div>
In other words, I want Lorem ipsum dolor sit amet.
rather than Editor's DescriptionLast updated: Lorem ipsum dolor sit amet.
In the provided XML document:
<div id="comment">
<div class="title">Editor's Description</div>
<div class="changed">Last updated: </div>
<br class="clear">
Lorem ipsum dolor sit amet.
</div>
the top element /div
has 4 children nodes that are text nodes. The first three of these four text-node
children are whitespace-only
. The last of these 4 text-node
children is the one that is wanted.
Use:
/div/text()[last()]
This is different from:
/div/text()
The latter may (depending on whether whitespace-only
nodes are preserved by the XML parser) select all 4 text nodes, but you only want the last of them.
An alternative is (when you don't know exactly which text-node
you want):
/div/text()[normalize-space()]
This selects all text-node-children
of /div
that are not whitespace-only
text nodes.
这篇关于只在节点中直接选择文本,而不是在子节点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!