Skip to content

XML XPath

XPath (XML Path Language) is a powerful query language used to select nodes (elements, attributes, text, etc.) from an XML document. Think of it as a way to specify addresses or paths to find specific pieces of information within the XML’s tree structure.

  • Node Selection: Defines parts of an XML document using path expressions.
  • Path Navigation: Uses a syntax reminiscent of file system paths to traverse the XML tree.
  • Built-in Functions: Provides a library of functions for string manipulation, numeric calculations, boolean logic, and node inspection.
  • Foundation for Other Technologies: A crucial component of XSLT (for transforming XML) and widely used in XQuery (for querying XML databases), programming languages (like Python, Java, JavaScript), and various XML tools.
  • W3C Standard: XPath is an official recommendation from the World Wide Web Consortium (W3C), with versions like XPath 1.0, 2.0, 3.0, and 3.1.

XPath expressions allow you to pinpoint specific nodes or sets of nodes within an XML document. The syntax is intuitive, especially if you’re familiar with directory paths.

XPath is integrated into many programming languages and platforms, including JavaScript (native browser support and libraries), Java (javax.xml.xpath), Python (lxml, xml.etree.ElementTree), PHP (DOMXPath), C#, and more, making it a versatile tool for XML data extraction.

XPath is fundamental to XSLT (Extensible Stylesheet Language Transformations). XSLT uses XPath expressions extensively to select the parts of an input XML document that need to be transformed.

Beyond XSLT, XPath is commonly used directly in code to query XML documents loaded into memory (e.g., using a DOM parser) to extract specific values or check for the existence of certain elements or attributes.

Consider the following sample XML document representing a small bookstore inventory:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB" available="true">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Here are some XPath expressions and what they select from the document above:

XPath ExpressionResult Description
/bookstore/bookSelects all book elements that are direct children of the bookstore root element.
//bookSelects all book elements anywhere in the document.
/bookstore/book[1]Selects the first book element under bookstore (index is 1-based).
/bookstore/book[last()]Selects the last book element under bookstore.
//titleSelects all title elements anywhere in the document.
//book/titleSelects all title elements that are direct children of a book element.
//book[@category=‘WEB’]Selects all book elements that have a category attribute with the value ‘WEB’.
//book[@available]Selects all book elements that have an available attribute (regardless of its value).
//title[@lang=‘en’]Selects all title elements that have a lang attribute with the value ‘en’.
/bookstore/book[price > 35.00]Selects book elements under bookstore whose price child element has a numeric value greater than 35.00.
//book[price > 35.00]/titleSelects the title elements of books whose price is greater than 35.00.
//author/text()Selects the text content of all author elements.

Learning XPath is essential for effectively working with XML data. Explore dedicated XPath tutorials and resources to understand its full capabilities, including axes, operators, and functions.