XPath vs XQuery

They are not competitors: XQuery 3.1 *contains* XPath 3.1. Every valid XPath expression is a valid XQuery, which is why a bare path like //album runs fine in an XQuery processor. The question is never which language is better — it is whether your task needs the things XQuery adds on top.

Example

XPath on the Music Playlist sample:

//album[count(tracks/track) > 3]/title

Pure XPath: it selects existing nodes from the document. That is all XPath can do — the nodes come back as they are.

Open it in the Sandbox

The same thing in XQuery

XQuery 3.1 on the Music Playlist sample:

<long-albums>{
  for $a in //album
  let $n := count($a/tracks/track)
  where $n > 3
  order by $n descending
  return <album tracks="{$n}">{string($a/title)}</album>
}</long-albums>

The same selection in XQuery — but now the result is a new document: a <long-albums> wrapper, an ordering, and a computed tracks attribute that exists nowhere in the source.

Open it in the Sandbox

The short answer

Use XPath when you want to *find* nodes in a document that already exists. Use XQuery when you want to *build* something — a different shape, a summary, a join across two documents, a report.

What each language can do
CapabilityXPath 3.1XQuery 3.1
Select nodes by path, axis, predicateYesYes
Full function library, maps, arrays, sequencesYesYes
Return nodes that already existYesYes
Construct new elements and attributesNoYes — direct and computed constructors
for / let / where / order by / returnfor and let onlyFull FLWOR
Declare namespaces, functions, variables up frontNoYes — the prolog
Branch on a node's typeNotypeswitch
Where it typically runsBrowsers, Selenium, XSLT, config filesXML databases and processors

Watch out for XPath 1.0

Most of the confusion in this comparison is not XPath versus XQuery at all — it is XPath 1.0 versus 3.1. XPath 1.0 is what browsers, Selenium locators, and most older tooling implement, and it has no sequences, no let, no for, and a much smaller function library.

If your expression has to run in a browser or a Selenium locator, you are writing 1.0 and neither 3.1 nor XQuery features are available to you. The Sandbox has a version toggle so you can tell a syntax error apart from a version problem in one click.

What XQuery is not

XQuery is not XSLT. Both transform XML, but XSLT is template- and rule-driven while XQuery reads as a programming language — and a document that arrives in an unpredictable shape is often easier in XSLT than in XQuery.

XQuery is also not a full data-manipulation language on its own. Updating expressions (insert, delete, replace) are a separate specification, and the engine here does not implement them — nor XQuery modules or fn:transform. The engine-support page lists every gap with a portable rewrite.

Keep going