XQuery Reference
XQuery 3.1 is a superset of XPath 3.1. This reference covers what XQuery adds on top: FLWOR expressions, element and attribute constructors, the prolog, and typeswitch.
FLWOR — For, Let, Where, Order by, Return — is the loop at the heart of XQuery. Where an XPath path expression can only select nodes in document order, a FLWOR binds variables, filters, groups, sorts, and then shapes one result per iteration. If you have ever wanted XPath to produce a sorted report, or one summary row per author, this is the construct that does it.
Constructors are how XQuery builds new XML instead of only selecting existing XML. You write literal markup straight into the query, drop into expressions with curly braces, or compute element and attribute names at runtime. This is the capability that turns a query language into a transformation language — and it is the main reason people move a job from XPath to XQuery.
The prolog is everything before the main expression: version and namespace declarations, global variables, and your own functions. It is what lets an XQuery grow past a one-liner into a maintainable transformation, and it is the piece with no XPath equivalent at all. Every prolog item ends with a semicolon.
XQuery inherits XPath's if/then/else and its type system, then adds typeswitch — a branch on what a value *is* rather than on a boolean test. Together with instance of, cast as and treat as, these are the tools for writing a query that survives contact with messy, irregular documents rather than assuming every node looks the same.
XQuery 3.1 is a strict superset of XPath 3.1, so nothing you already know is wasted: a bare path expression like /catalog/book is a perfectly valid, complete XQuery. What changes is what becomes possible — sorting, grouping, named functions, and building new XML. This section is the orientation map for deciding which one a job actually needs.
Task-shaped answers to the jobs people actually bring to XQuery: unique values, joins across documents, top-N lists, reshaping XML into a new format, and per-category counts. Each recipe shows the XQuery and, where one exists, the plain XPath equivalent — so you can see exactly where XPath runs out and XQuery takes over. Every example runs against a built-in sample in the Sandbox, so you can open it, change it, and watch the result.
The built-in functions you reach for most in real XQuery work, grouped by the job they do rather than by specification chapter. Every one of these is part of the shared XPath 3.1 function library, so the same call works in XPath mode too — what XQuery adds is the FLWOR and constructor machinery around them. Each entry says when you want the function, not just what it does.
This Sandbox evaluates XQuery 3.1 with FontoXPath, a conformant browser engine that nevertheless leaves a few clauses unimplemented. This page lists exactly what fails and, where one exists, the rewrite that works — so a query that errors here can be fixed in one edit rather than abandoned. Everything listed as unsupported is still valid XQuery 3.1 and will run in a server-side processor such as BaseX or Saxon.