Xpath 1.0 获取先前不是静态位置的元素值 [英] Xpath 1.0 get previous element value which is not static position
本文介绍了Xpath 1.0 获取先前不是静态位置的元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的html结构如下
标题 1
<br><br><div class="active"></div><h2>标题2</h2><br><br><div class="active"></div><div></div><div></div><div class="active"></div><h2>标题3</h2><br><br><div></div><div></div><h2>标题4</h2><br><br><div></div><div class="active"></div><div></div>
我想根据具有 active 类的 div 获取前一个 h2 的值
用于选择节点的我的 xpath 后代轴如下
//div[contains(@class, 'active')]
我尝试了 Xpath 的 祖先 和 前置 轴,但没有得到正确的结果
Xpath 1.0 是否可以实现
注意:我只能使用 Xpath 1.0 版
提前致谢
解决方案
这个 XPath 表达式:
//div[@class='active']/preceding-sibling::h2[1]
可以理解为
<块引用>选择每个活动
元素之前的第一个
元素."应该为您解决问题.
My html structure as below
<h2>Title 1</h2>
<br>
<br>
<div class="active"></div>
<h2>Title 2</h2>
<br>
<br>
<div class="active"></div>
<div></div>
<div></div>
<div class="active"></div>
<h2>Title 3</h2>
<br>
<br>
<div></div>
<div></div>
<h2>Title 4</h2>
<br>
<br>
<div></div>
<div class="active"></div>
<div></div>
I would like to get previous first h2's value according to divs which has got active class
My xpath descendants axes for selecting nodes as below
//div[contains(@class, 'active')]
I have tried ancestor and preceding axes of Xpath but not get right result
is this possible with Xpath 1.0
Note: I can use only Xpath version 1.0
Thank you in advance
解决方案
This XPath expression:
//div[@class='active']/preceding-sibling::h2[1]
which can be read as
"Select the first
<h2>
element before every active<div>
element."
should do the trick for you.
这篇关于Xpath 1.0 获取先前不是静态位置的元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文