Predicates

Predicates are for the purpose of finding some specific nodes or any node that contains some specific value. Predicates should be used in square brackets.

The following table shows some sample Xpath expressions and the results related with those expressions:

Path Expression Result
/publisher/product[1] This expression is used for selecting first element of product.
/publisher/product[last()] This expression is used for selecting the last product element.
/publisher/product[last()-1] This expression is used for selecting the latest product element.
/publisher/product[position()<3] This expression is used for selecting the first two product elements.
//title[@lang] This expression is used for selecting all of the title ...

Get R Web Scraping Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.