SyntaxStudy
Sign Up
XML XPath Syntax and Location Paths
XML Beginner 2 min read

XPath Syntax and Location Paths

XPath (XML Path Language) is a W3C standard for addressing parts of an XML document. It provides a syntax for writing expressions that select nodes or compute values from an XML tree. XPath 1.0 is the most widely implemented version, supported natively by all XSLT 1.0 processors, XQuery engines, and most XML APIs. XPath 2.0 and 3.1 added a full type system, sequence operations, and functions, but are primarily used within Saxon-based XSLT 2.0/3.0 environments. The fundamental concept in XPath is the location path, which selects a set of nodes from the tree. An absolute path starts with a forward slash (/) and navigates from the document root. A relative path starts from the context node. Each step in a path has three parts: an axis that specifies the tree relationship (child, parent, ancestor, descendant, following-sibling, etc.), a node test that filters by name or type, and zero or more predicates in square brackets that apply additional conditions. The abbreviated syntax is the form most commonly seen in practice. The child:: axis is implicit, so child::book and book are equivalent. The // abbreviation means descendant-or-self::node()/. The . abbreviation is self::node() and .. is parent::node(). Attribute nodes are selected with @. Together these shorthands make common XPath expressions concise: //book[@category='fiction']/title selects all title elements that are children of book elements with a category attribute value of fiction, anywhere in the document.
Example
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Sample document used in XPath examples below
-->
<library xmlns:dc="http://purl.org/dc/elements/1.1/">
    <book id="b001" category="fiction" lang="en">
        <title>The Great Gatsby</title>
        <author>F. Scott Fitzgerald</author>
        <year>1925</year>
        <price>12.99</price>
        <dc:subject>American literature</dc:subject>
    </book>
    <book id="b002" category="non-fiction" lang="en">
        <title>Clean Code</title>
        <author>Robert C. Martin</author>
        <year>2008</year>
        <price>35.00</price>
    </book>
    <magazine id="m001" category="tech">
        <title>Linux Journal</title>
        <year>2024</year>
    </magazine>
</library>

<!--
    XPath Expressions (results annotated):

    /library                    → root <library> element
    /library/book               → both <book> elements
    //title                     → all <title> elements (book + magazine)
    //book[@category='fiction']         → b001 only
    //book[year > 2000]                 → b002 only
    //book[@category='fiction']/title   → <title>The Great Gatsby</title>
    /library/*                          → book, book, magazine
    /library/book[1]                    → first book (b001)
    /library/book[last()]               → last book (b002)
    /library/book/@id                   → b001, b002 (attribute nodes)
    count(//book)                       → 2
    sum(//price)                        → 47.99
    //book[not(@lang)]                  → none (both have lang)
    /library/book[position() <= 2]      → b001, b002
    //title[contains(text(),'Code')]    → Clean Code
-->