How to join values using XPath concat?

The concat() function is an XPath expression is a string join function for multiple string values. It's useful when parsing an HTML document for a specific string through distributed multiple elements or attributes:

For example, let's see the below interactive XPath expression evaluation:

<html> <div> <h2>Coffee</h2> <span data-currency="USD">4.69</span> </div> </html>

Above, we contact the name and price elements into a single string. However, we can join any number of elements. Here's an example through two or more arguments:

<html> <div> <h2>Coffee</h2> <span data-currency="USD">4.69</span> </div></html>

Note that concat() only works with strings so it'll automatically try convert elements to their text() values. For further details on XPath selectors, including common tips and tricks, refer to our dedicated guide.

Parsing HTML with Xpath

Take a deep dive into XPath selectors through an expression cheatsheet and explore concepts using an interactive XPath tester. We'll also cover implementations, common idioms, and tips when it comes to XPath in web scraping.

xpath guide banner

Provided by Scrapfly

This knowledgebase is provided by Scrapfly data APIs, check us out! 👇