Gabriele B
Gabriele B

Reputation: 2685

Selecting an element after another without following axis in XPath

I have the following tree snippet:

<div class="pagesection">
  <div class="pagelinks floatleft">Pagine: [<strong>1</strong>]
    <a class="navPages" href="http://foo.com/2">2</a>
    <a class="navPages" href="http://foo.com/3">3</a>
  </div>
</div>

This is the navigation pane of a section of a website I need to scrap. So, I need to access the first A link (the next page) after the strong element (the current page).

I cannot use //DIV[@class="pagelinks"]/A[1] because when I'm for ex. on page 2, the first link of the pane is the 1st page. This would cause a loop in my scraper (it expects a "next page" link resulting from the expression). To be sure of navigating forward I must access the A link after the current page only.

Even worst, the scraper doesn't allow the use of preceding/following axis, but preceding-siblings and following-siblings seems to be supported, instead.

Which xpath expression I've to use for access that A link?

Upvotes: 1

Views: 96

Answers (1)

alecxe
alecxe

Reputation: 474191

You can get the first following-sibling::a:

//div[contains(@class, "pagelinks")]/strong/following-sibling::a[1]/text()

Demo (using xmllint tool):

$ xmllint index.html --xpath '//div[contains(@class, "pagelinks")]/strong/following-sibling::a[1]/text()'
2

Upvotes: 1

Related Questions