How to reverse expressions in XPath?

To reverse expression in XPath the not() function can be used which reverses any expression passed to it.

This is useful for creative negative predicates that are vital in many HTML parsing tasks. See this interactive example:

<!-- select only product data and ignore advertisements--> <article> <h1>Product Details:</h1> <div>price: 199</div> <div class="advertisement">Buy today?</div> <div>year: 2023</div> </article>

Above, we can use not() function to filter out special HTML elements like advertisements.

Question tagged: XPath

Related Posts

Ultimate XPath Cheatsheet for HTML Parsing in Web Scraping

Ultimate companion for HTML parsing using XPath selectors. This cheatsheet contains all syntax explanations with interactive examples.

Web Scraping With Ruby

Introduction to web scraping with Ruby. How to handle http connections, parse html files for data, best practices, tips and an example project.

Parsing HTML with Xpath

Introduction to xpath in the context of web-scraping. How to extract data from HTML documents using xpath, best practices and available tools.