views:

138

answers:

1

Sorry in advance if I don't explain this clearly. I will try my best to clarify what I am trying to do. Without further ado...Suppose you have a series of numbers on a page (separated by only a space) where brackets indicate the current page you are on.

It looks like this on the page:

[1] 2 3

The HTML looks like this:

<tr>
<td>

[<a href='link1.php'>1</a>] <a href='link2.php'>2</a> <a href='link3.php'>3</a> 

</td>
</tr>

I am simply trying to select the next page number based on the current. I imagine I need to use some form of following-sibling but all I have come up with is //tr/td/a/following-sibling::a[1], which is obviously incorrect. It selects 2 when on page 1 but not 3 when on page 2 as expected. I would have tried to use a[text()[contains(.,'[')]] to select the current-page but the brackets are outside the anchor as opposed to inside. Eek!?

It would be much appreciated if you could explain the thought process along with your solution instead of just pasting the answer. Looking forward to your help.

+2  A: 

The next page after 2, will be found by:

//tr/td/a[.='2']/following-sibling::a[1]

First we select the current page based on its string content //tr/td/a[.='2'], and from there select the first following-sibling.

To do this without knowing the current page, we will instead select the a element following the "]".

//tr/td/text()[']'=normalize-space(.)]/following-sibling::a[1]

And in case that there is no current page, we will default to the first a element.

//tr/td/( text()[']'=normalize-space(.)]/following-sibling::a | a )[1]
Lachlan Roche
Works wonderfully and makes perfect sense. Thanks a lot.