XPath - 与某些祖先不匹配的元素

时间:2014-03-08 00:51:31

标签: html xpath

我想计算所有没有链接作为祖先的后代元素的文本。

//*[string-length(normalize-space(//*[not(ancestor::a)])) > 10]

如果在此结构上进行测试,则返回[Get This Text]

<b>
   ignore
   <a>ignore</a>
   Get This Text
</b>

1 个答案:

答案 0 :(得分:2)

“计算文本”并不是很清楚你的意思,但是下面的表达式返回所有没有链接作为祖先且其规范化字符串值超过10个字符的元素:

//*[not(ancestor::a) and string-length(normalize-space()) > 10]

由于您希望表达式返回字符串'Get this text',您可能需要选择文本节点,而不是元素:

//text()[not(ancestor::a) and string-length(normalize-space()) > 10]
相关问题