# Negative indexing and slicing in Xpath

## 2017/04/25

In Python, you could do list[-1] to get the last element. So, how do you do it in XPATH? Say, I want to get the last link in the navigation that is made up of lots of <li\>s. To do that, you can use the following xpath:

last_link_xpath = '//ul[@class="pagination"]/li[last()-1]/a/text()'


Now, on to slicing, there’s a very useful StackOverflow question regarding this. To summarize it:

list[5:10]


in Python is equivalent to:

/*/bar[position() >= 5 and not(position() >= 10)]


Happy XPATHing and using Scrapy with it.