Cheat sheet
The syntax on one page. Every entry appears in a verified example on the tutorial pages; follow the link at the end of each row to see it run.
Paths
//book | book elements at any depth ex |
/library/book/title | child steps from the root ex |
//book[@lang = "es"] | filter by attribute ex |
//book[2] | second book child of each parent ex |
(//book)[last()] | last of the whole sequence ex |
ancestor::book, .. | up the tree ex |
following-sibling::author | sideways ex |
price/xs:decimal(.) | function as a step ex |
$titles ! upper-case(.) | map over a sequence ex |
Comparisons
$seq = "en" | true if any item matches ex |
$one eq "en" | exactly one item each side ex |
some $b in $books satisfies $b/price < 13 | existential ex |
every $b in $books satisfies $b/price > 0 | universal ex |
$a is $b, deep-equal($a, $b) | identity, value equality ex |
FLWOR
for $b in $books | iterate ex |
let $p := xs:decimal($b/price) | bind once ex |
where $p < 20 | filter ex |
order by $p descending | sort ex |
group by $author := string($b/author) | group ex |
for $b at $pos in ... | input position ex |
count $rank | number the results ex |
for $b allowing empty in ... | outer join ex |
for tumbling window $w in $s
start at $s when true()
end at $e when $e - $s eq 2 | batches ex |
for sliding window $w in $s ...
only end at $e when ... | moving windows ex |
return ... | result per binding ex |
Constructors
<li lang="{$b/@lang}">{ $t }</li> | direct, with enclosed expressions ex |
element { concat("h", $n) } { ... } | computed element name ex |
attribute id { $name } | computed attribute ex |
text { "Release notes" } | text node ex |
Functions and modules
declare function local:f($x as xs:integer)
as xs:integer { ... }; | user function ex |
function($x) { $x * $x } | inline function ex |
upper-case#1 | function reference ex |
for-each($s, $f), filter($s, $p), fold-left($s, 0, $f) | higher-order ex |
module namespace geo = "http://example.org/geo"; | library module ex |
import module namespace geo =
"http://example.org/geo" at "lib/geo.xqm"; | import ex |
Types and errors
xs:integer("42"), "2026-10-29" cast as xs:date | convert ex |
"4x2" castable as xs:integer | test conversion ex |
3 instance of xs:decimal | test type ex |
switch ($c) case "es" return ... default return ... | by value ex |
typeswitch ($i) case element() return ... | by type ex |
validate { $node } | schema validation ex |
try { ... } catch err:FORG0001 { $err:code } | catch by name ex |
catch * { $err:description } | catch all ex |
error(QName($ns, "app:E42"), "message") | raise ex |
Updates
copy $b := $book modify (...) return $b | update a copy ex |
insert node <tag/> into $b/tags | insert ex |
delete node $b/@lang | delete ex |
replace value of node $b/price with "9.99" | replace content ex |
rename node $b/author as "writer" | rename ex |
Full text
$p contains text "fox" | word search ex |
... "fox" using stemming | match word forms ex |
... "bear fox" any word | any of the words ex |
"brown" ftand ftnot "bear" | boolean ex |
"quick" ftand "fox" window 3 words | proximity ex |
"run.*" using wildcards | wildcards ex |
JSONiq (.jq)
{ "a": 1, "b": [ 1, 2 ], "c": null } | construct ex |
$o.customer.name, $o."ship to", $o.$key | key lookup ex |
$a[[2]] | array member, from 1 ex |
$a[] | all members ex |
keys($o), members($a), size($a) | introspection ex |
$people[$$.age ge 18] | context item ex |
parse-json(unparsed-text("orders.json")) | load JSON ex |
{| $o1, $o2 |} | merge objects ex |
case object, case null, case string | JSON types ex |
JSONiq updates
insert json { "k": 1 } into $o | add pairs ex |
append json "B7" into $o.items | add to array ex |
delete json $o.coupon | remove a key ex |
replace value of json $o.status with "shipped" | change a value ex |
rename json $o.customer as "client" | rename a key ex |
JSON in XQuery (.xq)
$o("name") | key lookup ex |
$a(2) | array member ex |
$a() | all members ex |
jn:null() | null ex |
replace value of json $o("a") with 2 | update ex |
Zorba
zorba -i -r -f -q query.xq | run a file, indented ex |
-e name:=Rodolfo | external variable ex |
variable $i := 1;
while ($i le 5) { $i := $i + 1; } | scripting ex |
ddl:create($name);
dml:insert-last($name, $items); | dynamic collection ex |
dml:delete(dml:collection($name)[...]) | remove from collection ex |
declare collection shop:products as element(product)*; | static collection ex |
idml:probe-index-point-value($idx, "tea") | index lookup ex |