ZorbaSource on GitHub

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

//bookbook elements at any depth ex
/library/book/titlechild 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::authorsideways 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 < 13existential ex
every $b in $books satisfies $b/price > 0universal ex
$a is $b, deep-equal($a, $b)identity, value equality ex

FLWOR

for $b in $booksiterate ex
let $p := xs:decimal($b/price)bind once ex
where $p < 20filter ex
order by $p descendingsort ex
group by $author := string($b/author)group ex
for $b at $pos in ...input position ex
count $ranknumber 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 2batches 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#1function 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:dateconvert ex
"4x2" castable as xs:integertest conversion ex
3 instance of xs:decimaltest 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 $bupdate a copy ex
insert node <tag/> into $b/tagsinsert ex
delete node $b/@langdelete 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 stemmingmatch word forms ex
... "bear fox" any wordany of the words ex
"brown" ftand ftnot "bear"boolean ex
"quick" ftand "fox" window 3 wordsproximity ex
"run.*" using wildcardswildcards ex

JSONiq (.jq)

{ "a": 1, "b": [ 1, 2 ], "c": null }construct ex
$o.customer.name, $o."ship to", $o.$keykey 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 stringJSON types ex

JSONiq updates

insert json { "k": 1 } into $oadd pairs ex
append json "B7" into $o.itemsadd to array ex
delete json $o.couponremove 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 2update ex

Zorba

zorba -i -r -f -q query.xqrun a file, indented ex
-e name:=Rodolfoexternal 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