XQuery vs XPath
Estimated study time: 11 minutes.
XPath and XQuery are closely related — XQuery actually builds on top of XPath — but they serve different purposes and have different levels of expressive power.
XPath: Selecting Nodes
//course[@level='beginner']/title
XPath is a path-based expression language. Given a document, it selects a set of matching nodes. It doesn't have loops, variables, or the ability to construct new output.
XQuery: Querying and Constructing Data
for $c in //course
where $c/@level = 'beginner'
order by $c/title
return <result>{$c/title/text()}</result>
XQuery uses XPath internally to select nodes (the //course part) but adds the ability to filter with full logic, sort, iterate, and construct entirely new XML output — things XPath cannot do on its own.
Key Differences
- Scope — XPath selects; XQuery selects, transforms, and constructs.
- Complexity — XPath expressions are short, single-line paths; XQuery supports full query programs.
- Output — XPath returns a node set; XQuery can output freshly constructed XML or HTML.
- Use case — XPath for simple node selection (e.g. inside XSLT); XQuery for database-style querying across documents.
A Note on XPath Versions
The comparison above describes XPath 1.0, which is still what browsers, Selenium and many libraries support. XPath 2.0 and later versions added for, let and if expressions, sequences and many more functions. Even so, XPath still cannot build new elements the way XQuery does, so the core difference remains: XPath selects and calculates, while XQuery can also construct and reshape documents.
Both Languages on the Same Document
Take this small book catalogue saved as books.xml:
<catalog>
<book id="b1" level="beginner"><title>XML Basics</title><pages>180</pages></book>
<book id="b2" level="advanced"><title>XSLT Deep Dive</title><pages>420</pages></book>
<book id="b3" level="beginner"><title>JSON vs XML</title><pages>90</pages></book>
</catalog>
The document is deliberately tiny so you can check every result by eye before trusting the expressions on real data. Useful XPath expressions against it:
count(//book[@level='beginner']) (: 2 :)
//book[pages > 100]/title (: XML Basics, XSLT Deep Dive :)
//book[contains(title, 'XML')]/@id (: b1, b3 :)
(//book)[1]/title (: first book; positions start at 1 :)
An XQuery that builds a brand-new report from the same data:
let $doc := doc("books.xml")
let $beginner := $doc//book[@level = 'beginner']
return
<report total="{count($beginner)}"
pages="{sum($beginner/pages)}">
{
for $c in $beginner
order by xs:integer($c/pages) descending
return <item id="{$c/@id}">{data($c/title)}</item>
}
</report>
How a FLWOR Expression Runs
- for loops over each node in a sequence, binding it to a variable.
- let binds a value once, such as a filtered set or a total.
- where filters the items, like SQL's WHERE.
- order by sorts the results; cast to a number when sorting numeric text.
- return produces output for each item, including new XML built with curly-brace expressions.
A FLWOR expression needs at least one for or let clause and a return; the other clauses are optional, and you can repeat for and let as often as you need. Filtering early in where, or directly in the path predicate, keeps queries on large documents quick.
Because doc() can open several files, XQuery can also join documents, for example matching book IDs in one file with order records in another.
Where You Will Meet Each One
| Context | Language used |
|---|---|
| Selenium locators, browser developer tools | XPath 1.0 |
| XSLT stylesheets | XPath inside match and select attributes |
Java javax.xml.xpath, Python lxml | XPath |
| Saxon, BaseX, eXist-db, MarkLogic | XQuery |
SQL Server xml columns | XQuery through .query(), .value() and .exist() |
To try these queries yourself, a free processor such as BaseX or the Saxon home edition runs XQuery files directly, while any browser's developer console can test XPath with $x("//book/title").
Common mistakes with both include forgetting that a document with a default namespace needs a declared prefix in every path, using // everywhere on huge documents when a direct path would be faster, comparing numbers stored as text without casting, and assuming positions start at 0 when they start at 1.