除“n"之外的所有嵌套文本的 XPath嵌套标签的文本 [英] XPath for all nested text except "n" nested tags' text
本文介绍了除“n"之外的所有嵌套文本的 XPath嵌套标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 html.
<h3>标题</h3><p>一些东西</p><p>其他东西</p><p>其他东西</p><p>不必要的东西</p><p>其他不必要的东西</p>
到目前为止我已经写了这个表达式.
//div[@id="content"]//text()
哪个有效,但我想要的不是提取最后 2 个 <p>
元素的文本,因为这是不必要的.我试着写这个...
//div[@id="content"]/p[not(position() > last() - 2)]//text()
没有按预期工作.然后我尝试了这个...
//div[@id="content"]/[not(self::p[position() > last() - 2])]//text()
这也不起作用.
解决方案
此表达式返回您感兴趣的文本节点:
//div[@id="content"]/*[not(self::p and position() > last() - 2)]//文本()
您在 /
之后缺少一个 *
.
I have following html.
<div id="content">
<h3>Title</h3>
<p>Some stuff</p>
<p>other stuff</p>
<p>other other stuff</p>
<p>unnecessary stuff</p>
<p>other unnecessary stuff</p>
</div>
I have written this expression so far.
//div[@id="content"]//text()
Which works but what I want is not to extract the text of last 2 <p>
elements because that is unnecessary. I tried to write this...
//div[@id="content"]/p[not(position() > last() - 2)]//text()
Which did not work as expected. Then I tried this...
//div[@id="content"]/[not(self::p[position() > last() - 2])]//text()
Which also did not work.
解决方案
This expression returns the text nodes you are interested in:
//div[@id="content"]/*[not(self::p and position() > last() - 2)]//text()
You were missing an *
after the /
.
这篇关于除“n"之外的所有嵌套文本的 XPath嵌套标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文