xpath按索引获取元素

Tho*_*mas 15 php xml xslt xpath

我有下面的xpath表达式

//div[@class="post-content"]//img
Run Code Online (Sandbox Code Playgroud)

它在html页面上运行,扫描图像.上面的查询返回了很多图像,但我只想要列表中的第二个.

我试过了

//div[@class="post-content"]//img[1] and
//div[@class="post-content"]//img[position()=1]
Run Code Online (Sandbox Code Playgroud)

没有运气

我该怎么做?

谢谢

Kir*_*huk 40

因此,XPath索引从1个位置开始

//div[@class="post-content"]//img[2]
Run Code Online (Sandbox Code Playgroud)

如果你必须选择每个第二个img,应该正常工作div[@class="post-content"].如果您必须img从所有图像中仅选择第二个div[@class="post-content"],请使用:

(//div[@class="post-content"]//img)[2]
Run Code Online (Sandbox Code Playgroud)

  • 出色的。虽然我没说清楚,但你还是打中了。第二个就是我一直在寻找的。多谢 (2认同)

Col*_*ard 9

XPath中的索引是从1开始的,而不是从0开始的.尝试

(//div[@class="post-content"]//img)[position()=2]
Run Code Online (Sandbox Code Playgroud)

  • 当然XPath索引是基于1的...什么是地狱XPath? (2认同)