Ruslan
Ruslan

Reputation: 1301

How to select the specific sibling of an ancestor using XPath

I have the following HTML structure:

<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>

Using Nokogiri and custom XPath functions I am able to select the <span> element containing context that matches the regex. I am forced to do it this way since Nokogiri is using XPath 1.0 and there is no support for the matches selector:

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

Having the span node selected, how do I select the table that is visually following the span?

Upvotes: 2

Views: 226

Answers (1)

Sicco
Sicco

Reputation: 6271

I use the contains function to match the text. Then use following::table to find the table following this span tag.

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

Upvotes: 2

Related Questions