你需要了解difference between text() nodes and string values in XPath。
-
text() 在 XPath 中选择 text nodes。 br 元素显示在
您在父元素中选择表单混合内容:text()
节点和元素混合在一起。
-
string() 是一个 XPath 函数,它返回 XPath 表达式的 string value。要获取忽略 br 元素的字符串,请选择
父 div 并通过 string() 直接获取其字符串值
或通过使用 a 中的表达式隐式获取其字符串值
隐含转换为字符串的上下文。
在这样的背景下,你的陈述,
我想找到直接包含文本的元素,而文本是
超过 140 个字符和整个元素的文本应该是
选中(有时文本在 span 内更远)。
可以改写为
我想查找具有text() 子节点且其字符串值的长度大于140 的元素。
让我们看一些示例 XML,
<r>
<a>This is a <b>test</b> of mixed content.</a>
<c>asdf asdf asdf asdf</c>
<d>asdf asdf</d>
</r>
然后让我们将 140 减少到 8 以使其更易于管理,然后
//*[text()][string-length() > 7]
捕获重新表述的需求并选择四个元素:
<r>
<a>This is a <b>test</b> of mixed content.</a>
<c>asdf asdf asdf asdf</c>
<d>asdf asdf</d>
</r>
<a>This is a <b>test</b> of mixed content.</a>
<c>asdf asdf asdf asdf</c>
<d>asdf asdf</d>
注意它没有选择b,因为它的字符串值的长度小于7个字符。
还要注意,r 被选中是因为元素之间只有空格 text()。要消除此类元素,请向text() 添加一个额外的谓词:
//*[text()[normalize-space()]][string-length() > 7]
那么,只会选择a、c和d。
如果你只想要文本,在 XPath 1.0 中你可以集体取字符串值:
string(//*[text()[normalize-space()]][string-length() > 7])
如果您想要一个字符串集合,在 XPath 1.0 中,您需要通过调用 XPath 的语言对元素进行迭代,但在 XPath 2.0 中,您可以在末尾添加 string() 步骤:
//*[text()[normalize-space()]][string-length() > 7]/string()
获取三个独立字符串的序列:
This is a test of mixed content.
asdf asdf asdf asdf
asdf asdf