Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select the second last item in a xpath query?

Tags:

xpath

I'm new to xpath and I understand how to get a range of values in xpath:

/bookstore/book[position()>=2 and position()<=10]

but in my case, I need to get above 2 and one less then the total(so if there's 10 then I need 9, or if there's 5, I need up to the 4th spot). I'm applying my code to different pages and the number of entries is not always the same.

In python, I could do something like book[2:-2], but I'm unsure if I can do this within xpath.

like image 911
Lostsoul Avatar asked Apr 25 '14 01:04

Lostsoul


2 Answers

In my case this was working for me to get last but one element

/bookstore/book[position() = (last() - 1)]
like image 179
Marcin Dykas Avatar answered Sep 21 '22 12:09

Marcin Dykas


You can use last() which represents the last item in the context:

/bookstore/book[position()>=2 and position() <= (last() - 1)]
like image 38
helderdarocha Avatar answered Sep 18 '22 12:09

helderdarocha