如何使用XPATH获取标签之间的元素



我需要获取文章的每个小标题及其文本。因为每个副标题都在里面,我需要得到第一个和第二个之间的所有内容。然后我将在第二和第三之间进行操作,直到完成。

结构类似如下:

<article>
<p> introducion </p>
<h3>1. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>
<h3>2. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>
<h3>3. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>
</article>

目前我可以像这样进入第一个副标题://h3[1]

但是我怎么能得到第一个和第二个之间的所有东西??

这个XPath表达式得到//h3[1]//h3[2]之间的节点,包括

//article/*[position()>= count(//h3[1]/preceding-sibling::*)+1 and position()<= count(//h3[2]/preceding-sibling::*)+1]

浏览器控制台的结果

$x('//article/*[position()>= count(//h3[1]/preceding-sibling::*)+1 and position()<= count(//h3[2]/preceding-sibling::*)+1]')
Array(4) [ h3, p, div, h3]
0: <h3>​
1: <p>​
2: <div>​
3: <h3>
length: 4

最新更新