XPath选择两个标题之间的所有元素? [英] XPath to select all elements between two headings?

查看:548
本文介绍了XPath选择两个标题之间的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<h2>Headline 1</h2>
<p>some text</p>
<p>some more text</p>
<ul>
<li>list item 1</li>
<li>list item 2</li>
</ul>
<p>more text</p>
<h2>Headline 2</h2>

我在网页上有上述内容,我希望能够在第一个 h2 包含文本'标题1',但不包括 h2 包含文字'标题2'

I have the above in a webpage and I want to be able to target all elements following the first h2 that contains the text 'Headline 1' up to but NOT including the element h2 that contains the text 'Headline 2'.

我可以定位像这样的元素:

I can target the elements like this:

//*[count(preceding-sibling::hr)=1]

但这并非特定于文本包含,所以如果页面曾经改变,那么xpath可能指向完全不同的东西。

but this is not specific to the text contained and so if the page ever changed then the xpath could be pointing to something totally different.

我想用sudo代码术语是这样的:

What I would like in sudo code terms is this:


给出了标题'标题1'
标题'标题2'包括'标题1'

这是否可能?

推荐答案

此XPath,

This XPath,

//*[    preceding-sibling::h2[. = 'Headline 1'] 
    and following-sibling::h2[. = 'Headline 2']]

会选择 h2 s,字符串值为'标题1''标题2'

<p>some text</p>
<p>some more text</p>
<ul>
<li>list item 1</li>
<li>list item 2</li>
</ul>
<p>more text</p>






Andersson在评论中指出OP希望第一个 h2 包含在选择中。

Andersson的初步想法可行:

Andersson's initial thought would work:

//h2[. = 'Headline 1'] |
//*[    preceding-sibling::h2[. = 'Headline 1'] 
    and following-sibling::h2[. = 'Headline 2']]

以下是另一种方式:

Here's another way:

//*[self::h2[. = 'Headline 1']
    or (    preceding-sibling::h2[. = 'Headline 1'] 
        and following-sibling::h2[. = 'Headline 2']]

或者,可能是理想的方式:

Or, probably the ideal way:

//h2[. = 'Headline 2']
    /preceding-sibling::*[not(following-sibling::h2[. = 'Heading 1'])]

,因为它避免了必须指定'Heading 1'两次。

because it avoids having to specify 'Heading 1' twice.

这篇关于XPath选择两个标题之间的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆