本文介绍了XPath子集选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下XML(实际上是HTML):
I have the following XML (which is actually HTML):
<html>
<h4>something</h4>
<p>a</p>
<p>b</p>
<h4>otherthing</h4>
<p>c</p>
</html>
XPath可以选择跟随第一个h4节点的兄弟节点的p节点,但不是继第二个h4节点的兄弟姐妹(仅选择p节点a和b)?
Can a XPath selects the "p" nodes that are following siblings of the first "h4" node but not following siblings of second "h4" node (selecting "p" node a & b only)?
推荐答案
/ p>
My take
//p[preceding-sibling::h4[1] and not(preceding-sibling::h4[position() > 1])]
查找所有p元素,它们是第一个h4的兄弟,但不是任何兄弟其他h4在同一轴上
finds all p elements which are siblings of the first h4 but not siblings of any other h4 on the same axis
另类
//h4[1]/following-sibling::p[count(preceding-sibling::h4) = 1]
找到第一个h4元素的所有后面的p元素,它们确实有一个前面的h4元素
finds all following p element of the first h4 element that do have exactly one preceding h4 element
这篇关于XPath子集选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!