Using XPath: An XPath Tutorial

Extracting data from websites has become extremely important as businesses seek ways to improve their operations, monitor competitors, detect changes in trends, automate systems, and much more. One of the ways to get hard to reach data is XPath. If you’re seeking to engage in data extraction, becoming familiar with XPath is useful to get hidden data. This XPath tutorial will help with that.

What is XPath?

XPath stands for XML Path Language. It’s a large part of the XSLT standard and a type of query language used to select nodes within an XML document. One of the most common uses for XPath is to extract data. XPath essentially extracts data from XML or HTML in its raw form. With more than 200 functions built into it, XPath proves to be a versatile tool for big data. Some of those functions include searching for and computing data such as numeric values, sequence manipulation, a comparison of dates and times, and more.

XPath first got published back in 1999, and since then, updated versions have come out in 2007, 2014, and 2017. At the moment, though, the original version is the one most commonly used and available. Newer versions mainly focus on adding new data types and support for new functions.

Working With XPath

To better understand what XPath is exactly, just concentrate on the name. Think of XPath as a type of path, one that contains directions to look for a specific part of the coding on a webpage. By using this type of syntax, it’s able to navigate through the nodes found within the document. In a sense, XPath uses these path expressions for selecting the right nodes to look through and extract data from.

Those unfamiliar with XML may have difficulty picturing how this works. Perhaps one of the best ways to understand the function of XPath is to think of the path expressions it uses like a regular computer file system. For example, a computer file will usually contain more files and data. Just follow the file names and you’ll trace a path to the data you’re looking for. The same principle applies to when XPath searches for data on a website. By looking through the nodes, XPath can pinpoint where the data is and extract it.

Every element within an XPath data model is a node. The order in which the nodes appear is congruent with how they appear in the source code. This makes it easier to create an XPath expression to use to query data.

Creating a path requires understanding the nodes within the source code. Once you identify the nodes, you’ll start with a root node, then navigate further to a different node, usually separated with the forward-slash ( / ) character. It is through this that you can eventually get to the element of data you wish to extract from the website. When repeated with multiple expressions over multiple websites, you can filter out the unneeded data and extract only what you want.

Examples of XPath:

./author – All <author> elements within the current context.

//author – All <author> elements in the document.

author/first-name – All <first-name> elements that are children of an <author> element.

author/* – All elements that are the children of <author> elements.

XPath basically acts like a road map, giving directions that will help you wind up at the destination you want to go. Unlike other types of languages, XPath defines the route to take instead of searching for a specific value or sequence.

XPath isn’t always the easiest thing to use. When creating your own paths, you’ll need to create one for every website you want to extract data from. When using XPath, it does help to have at least some familiarity with coding languages like HTML, XML, and JavaScript. This is particularly helpful since XPath can be also used with a variety of programming languages like Java, Python, C, C++, PHP, and many more along with frameworks like Selenium, QTP, and Protractor.

Why Use XPath?

As mentioned above, XPath is best used for extracting data from coding languages, but there are other methods out there which claim the same thing. So why use XPath specifically? Well, there are a number of use cases where XPath comes in handy when extracting data from a website. One common case is when the data is hidden. This can often happen when the data appears in places like a dropdown menu. That data isn’t always easy to find and readily visible, but it will all appear in the page’s code. Another use case includes when the data moves on the website. By using XPath, you’re able to anchor your search through a specific phrase and follow that data no matter where it moves to. A third possible use case is when the data you’re looking for appears outside of the initial search results. These are just a few XPath examples where it can quickly turn into a vital tool that can mean the difference between a successful extraction or a failure.

XPath and Import.io

You can extract data using Import.io without ever knowing XPath. But, if you are more technical and want to get at data that may be hidden from view on a website, XPath is a great way to do it. By creating your own paths in Import.io, you’ll be able to find the data you’re looking for, or you can use the paths already generated and available through the app. Either way, you’ll have valuable information at your fingertips to help your organization grow.