使用 Nokogiri 在两个元素之间遍历不是子元素的元素 [英] Traverse elements that aren't children between two elements with Nokogiri

查看:26
本文介绍了使用 Nokogiri 在两个元素之间遍历不是子元素的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Nokogiri,我试图找出选择与其他两个 div 元素之间的 css 类匹配的 div 元素的最佳方法.这是我正在使用的一些示例 HTML:

<span>今天</span>

<div class="random"></div><div class="preferred"></div><div class="preferred"></div><div class="preferred"></div><div class="random"></div><div class="日期"><span>明天</span>

我想要的是基本上返回 divs 其类在 divs 与date"类之间首选"的.

我可以用以下方式抓住我的起点:

doc.at("span:contains('Today')").parent

但是,我不确定在下一个 div.date 之前获取基于 classnext_elements 的最佳方法.

解决方案

我正在应用 Kaysian 方法 用于获取集合的交集(如 这个问题的重复链接)在您的特定场景(而不是考虑您未包含在问题中的任何其他外部环境).

您需要定义两个集合:

  1. A: //div[preceding-sibling::div[@class='date']](所有 div 元素的前面有一个兄弟 date 类的 div.)
  2. B: //div[following-sibling::div[@class='date']](所有具有以下兄弟 div 元素date 类的 div.)

这两组的交集是您问题的解决方案.Kaysian 方法的公式为:A [ count(. | B) = count(B) ].将其应用于您的问题,您需要的结果是:

//div[preceding-sibling::div[@class='date']][count(.|//div[following-sibling::div[@class='date']])= count(//div[following-sibling::div[@class='date']]) ]

这将选择以下元素:

<div class="preferred"></div><div class="preferred"></div><div class="preferred"></div><div class="random"></div>

如果您在其他 <div class="date">元素的全局上下文中应用此表达式,您将不得不调整它并更改您的集合,以便它们唯一地标识边界元素.

Using Nokogiri, I'm trying to figure out the best way to select div elements that match a css class between two other div elements. Here's some sample HTML of what I'm working with:

<div class="date">
  <span>Today</span>
</div>
<div class="random"></div>
<div class="preferred"></div>
<div class="preferred"></div>
<div class="preferred"></div>
<div class="random"></div>
<div class="date">
  <span>Tomorrow</span>
</div>

What I want is to basically return the divs whose class is "preferred" between the divs with "date" class.

I can grab my starting point with something like:

doc.at("span:contains('Today')").parent

However, I'm not sure the best way to get its next_elements based on class up until the next div.date.

解决方案

I'm applying the Kaysian method for obtaining the intersection of a set (as described in the duplicate link of this question) in the context of your specific scenario (and not considering any other external context which you didn't include in your question).

You need to define two sets:

  1. A: //div[preceding-sibling::div[@class='date']] (all div elements which have a preceding sibling div of the date class.)
  2. B: //div[following-sibling::div[@class='date']] (all div elements which have a following sibling div of the date class.)

The intersection of those two sets is the solution to your problem. The formula of the Kaysian method is: A [ count(. | B) = count(B) ]. Applying that to your problem, the result you need is:

//div[preceding-sibling::div[@class='date']][count( . | //div[following-sibling::div[@class='date']] ) = count(//div[following-sibling::div[@class='date']] ) ]

This will select the following elements:

<div class="random"></div>
<div class="preferred"></div>
<div class="preferred"></div>
<div class="preferred"></div>
<div class="random"></div>

If you apply this expression in a global context where there are other <div class="date"> elements, you will have to adapt it and change your sets so that they uniquely identify the bordering elements.

这篇关于使用 Nokogiri 在两个元素之间遍历不是子元素的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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