Vikas Kyatannawar
Vikas Kyatannawar

Reputation: 136

Xpath: How to select following siblings until a similar current node

<tr><strong>dynamic title</strong></tr>
<tr>1</tr>
<tr>2</tr>
<tr>3</tr>
<tr><strong>static title</strong></tr>
<tr>4</tr>
<tr>5</tr>
<tr><strong>dynamic title</strong></tr>
<tr>6</tr>
<tr>7</tr>
<tr><strong>dynamic title</strong></tr>
<tr>8</tr>
<tr>9</tr>

Given the above scenario, I want to select the values 4 and 5 using the static title as a marker.

//tr[preceding-sibling::tr[strong][contains(.,"static title")]] or //tr[strong[contains(.,"static title")]]/following-sibling::tr

This will select 6,7,8 and 9 too. Is there a way to make it select the first preceding-sibling with strong and then check for the contains? Or maybe we can use count() somehow?

Upvotes: 1

Views: 157

Answers (2)

JaSON
JaSON

Reputation: 4869

This looks little messy, but should work:

//tr[contains(strong,"static title")]/following-sibling::tr[strong][1]/preceding-sibling::tr[preceding-sibling::tr[contains(strong,"static title")]]

Upvotes: 0

choroba
choroba

Reputation: 241748

You can insert [1] after strong which means "the first strong preceding sibling must be static":

//tr[preceding-sibling::tr[strong][1][contains(.,"static title")]] 

Upvotes: 2

Related Questions