如何使用 XPath select 祖先的特定兄弟姐妹

How to select the specific sibling of an ancestor using XPath

我有以下 HTML 结构:

<p>
  <!-- Span can be any level deep -->
  <span>
    Some text
  </span>
</p>

<!-- Any number of different elements between span and table -->
<p></p>
<div></div>

<table>
  <tr>
    <td></td>
  </tr>
</table>

使用 Nokogiri 和自定义 XPath 函数,我能够 select <span> 包含与正则表达式匹配的上下文的元素。我不得不这样做,因为 Nokogiri 使用的是 XPath 1.0,并且不支持 matches selector:

@doc.xpath("//span[regex_match(text(), '/some text/i')]")

有了 span 节点 select,我如何 select 在视觉上跟随 span 的 table?

我使用contains函数来匹配文本。然后使用 following::table 查找此 span 标签后的 table。

@doc.xpath("//span[contains(text(), 'Some text')]/following::table")