除“n”嵌套标签文本外的所有嵌套文本的XPath

时间:2013-01-31 07:41:08

标签: xpath

我有以下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>

到目前为止,我已经写过这个表达。

//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()

哪个也行不通。

1 个答案:

答案 0 :(得分:2)

此表达式返回您感兴趣的文本节点:

//div[@id="content"]/*[not(self::p and position() > last() - 2)]//text()

*之后您错过了/

相关问题