XPath 轴,获取所有后续节点,直到 [英] XPath axis, get all following nodes until

查看:50
本文介绍了XPath 轴,获取所有后续节点,直到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 HTML 示例:

I have the following example of HTML:

<!-- lots of html -->
<h2>Foo bar</h2>
<p>lorem</p>
<p>ipsum</p>
<p>etc</p>

<h2>Bar baz</h2>
<p>dum dum dum</p>
<p>poopfiddles</p>
<!-- lots more html ... -->

我希望提取Foo bar"标题后面的所有段落,直到到达Bar baz"标题(Bar baz"标题的文本未知,因此很遗憾我无法使用答案布吉曼提供).现在我当然可以使用诸如 //h2[text()='Foo bar']/following::p 之类的东西,但这当然会抓取所有 段落之后标题.所以我可以选择遍历节点集并将段落推入一个数组,直到文本与下一个标题的文本匹配,但老实说,这从来没有像在 XPath 中那样酷.

I'm looking to extract all paragraphs following the 'Foo bar' header, until I reach the 'Bar baz' header (the text for the 'Bar baz' header is unknown, so unfortunately I can't use the answer provided by bougyman). Now I can of course using something like //h2[text()='Foo bar']/following::p but that of course will grab all paragraphs following this header. So I have the option to traverse the nodeset and push paragraphs into an Array until the text matches that of the next following header, but let's be honest, that's never as cool as being able to do it in XPath.

有没有一种方法可以做到这一点,但我缺少这种方法?

Is there a way to do this that I'm missing?

推荐答案

使用:

(//h2[. = 'Foo bar'])[1]/following-sibling::p
   [1 = count(preceding-sibling::h2[1] | (//h2[. = 'Foo bar'])[1])]

如果保证每个 h2 都有一个不同的值,这可以简化为:

In case it is guaranteed that every h2 has a distinct value, this may be simplified to:

//h2[. = 'Foo bar']/following-sibling::p
   [1 = count(preceding-sibling::h2[1] | ../h2[. = 'Foo bar'])]

这意味着:选择其字符串值的 h2(文档中的第一个或唯一一个)的兄弟元素之后的所有 p 元素是 'Foo bar' 并且所有这些 p 元素的第一个前面的兄弟 h2 正是 h2(第一个或唯一一个在文档中)其字符串值为'Foo bar'`.

This means: Select all p elements that are following siblings of the h2 (first or only one in the document) whose string value is 'Foo bar' and also the first preceding sibling h2 for all these p elements is exactly the h2(first or only one in the document) whose string value is'Foo bar'`.

这里我们使用一种方法来判断两个节点是否相同:

count($n1 | $n2) = 1

is true() 正是当节点 $n1$n2 是同一个节点时.

is true() exactly when the nodes $n1 and $n2 are the same node.

这个表达式可以推广:

$x/following-sibling::p
       [1 = count(preceding-sibling::node()[name() = name($x)][1] | $x)]

选择 $x 指定的任何节点的所有紧随其后的兄弟节点".

这篇关于XPath 轴,获取所有后续节点,直到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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