# 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](map.md#xpath) |
| `/library/book/title` | child steps from the root [ex](xquery.md#paths) |
| `//book[@lang = "es"]` | filter by attribute [ex](map.md#xpath) |
| `//book[2]` | second book child of each parent [ex](xquery.md#predicates) |
| `(//book)[last()]` | last of the whole sequence [ex](xquery.md#predicates) |
| `ancestor::book`, `..` | up the tree [ex](xquery.md#axes) |
| `following-sibling::author` | sideways [ex](xquery.md#axes) |
| `price/xs:decimal(.)` | function as a step [ex](xquery.md#values) |
| `$titles ! upper-case(.)` | map over a sequence [ex](xquery.md#values) |

## Comparisons

|  |  |
|----|----|
| `$seq = "en"` | true if any item matches [ex](xquery.md#comparisons) |
| `$one eq "en"` | exactly one item each side [ex](xquery.md#comparisons) |
| `some $b in $books satisfies $b/price < 13` | existential [ex](xquery.md#comparisons) |
| `every $b in $books satisfies $b/price > 0` | universal [ex](xquery.md#comparisons) |
| `$a is $b`, `deep-equal($a, $b)` | identity, value equality [ex](map.md#nodes) |

## FLWOR

|  |  |
|----|----|
| `for $b in $books` | iterate [ex](xquery.md#flwor) |
| `let $p := xs:decimal($b/price)` | bind once [ex](xquery.md#flwor) |
| `where $p < 20` | filter [ex](xquery.md#flwor) |
| `order by $p descending` | sort [ex](xquery.md#flwor) |
| `group by $author := string($b/author)` | group [ex](xquery.md#group) |
| `for $b at $pos in ...` | input position [ex](xquery.md#count) |
| `count $rank` | number the results [ex](xquery.md#count) |
| `for $b allowing empty in ...` | outer join [ex](xquery.md#joins) |
| `for tumbling window $w in $s start at $s when true() end at $e when $e - $s eq 2` | batches [ex](xquery.md#windows) |
| `for sliding window $w in $s ... only end at $e when ...` | moving windows [ex](xquery.md#windows) |
| `return ...` | result per binding [ex](xquery.md#flwor) |

## Constructors

|  |  |
|----|----|
| `<li lang="{$b/@lang}">{ $t }</li>` | direct, with enclosed expressions [ex](xquery.md#constructors) |
| `element { concat("h", $n) } { ... }` | computed element name [ex](xquery.md#constructors) |
| `attribute id { $name }` | computed attribute [ex](xquery.md#constructors) |
| `text { "Release notes" }` | text node [ex](xquery.md#constructors) |

## Functions and modules

|  |  |
|----|----|
| `declare function local:f($x as xs:integer) as xs:integer { ... };` | user function [ex](xquery.md#functions) |
| `function($x) { $x * $x }` | inline function [ex](xquery.md#hof) |
| `upper-case#1` | function reference [ex](xquery.md#hof) |
| `for-each($s, $f)`, `filter($s, $p)`, `fold-left($s, 0, $f)` | higher-order [ex](xquery.md#hof) |
| `module namespace geo = "http://example.org/geo";` | library module [ex](xquery.md#modules) |
| `import module namespace geo = "http://example.org/geo" at "lib/geo.xqm";` | import [ex](xquery.md#modules) |

## Types and errors

|  |  |
|----|----|
| `xs:integer("42")`, `"2026-10-29" cast as xs:date` | convert [ex](xquery.md#types) |
| `"4x2" castable as xs:integer` | test conversion [ex](xquery.md#types) |
| `3 instance of xs:decimal` | test type [ex](xquery.md#types) |
| `switch ($c) case "es" return ... default return ...` | by value [ex](xquery.md#types) |
| `typeswitch ($i) case element() return ...` | by type [ex](map.md#items) |
| `validate { $node }` | schema validation [ex](xquery.md#schema) |
| `try { ... } catch err:FORG0001 { $err:code }` | catch by name [ex](xquery.md#errors) |
| `catch * { $err:description }` | catch all [ex](xquery.md#errors) |
| `error(QName($ns, "app:E42"), "message")` | raise [ex](xquery.md#errors) |

## Updates

|  |  |
|----|----|
| `copy $b := $book modify (...) return $b` | update a copy [ex](xquery.md#updates) |
| `insert node <tag/> into $b/tags` | insert [ex](xquery.md#updates) |
| `delete node $b/@lang` | delete [ex](xquery.md#updates) |
| `replace value of node $b/price with "9.99"` | replace content [ex](xquery.md#updates) |
| `rename node $b/author as "writer"` | rename [ex](xquery.md#updates) |

## Full text

|  |  |
|----|----|
| `$p contains text "fox"` | word search [ex](xquery.md#fulltext) |
| `... "fox" using stemming` | match word forms [ex](xquery.md#fulltext) |
| `... "bear fox" any word` | any of the words [ex](xquery.md#fulltext) |
| `"brown" ftand ftnot "bear"` | boolean [ex](xquery.md#fulltext) |
| `"quick" ftand "fox" window 3 words` | proximity [ex](xquery.md#fulltext) |
| `"run.*" using wildcards` | wildcards [ex](xquery.md#fulltext) |

## JSONiq (.jq)

|  |  |
|----|----|
| `{ "a": 1, "b": [ 1, 2 ], "c": null }` | construct [ex](jsoniq.md#construct) |
| `$o.customer.name`, `$o."ship to"`, `$o.$key` | key lookup [ex](jsoniq.md#navigate) |
| `$a[[2]]` | array member, from 1 [ex](jsoniq.md#navigate) |
| `$a[]` | all members [ex](jsoniq.md#navigate) |
| `keys($o)`, `members($a)`, `size($a)` | introspection [ex](jsoniq.md#functions) |
| `$people[$$.age ge 18]` | context item [ex](jsoniq.md#context) |
| `parse-json(unparsed-text("orders.json"))` | load JSON [ex](jsoniq.md#load) |
| `{| $o1, $o2 |}` | merge objects [ex](jsoniq.md#merge) |
| `case object`, `case null`, `case string` | JSON types [ex](jsoniq.md#types) |

## JSONiq updates

|  |  |
|----|----|
| `insert json { "k": 1 } into $o` | add pairs [ex](jsoniq.md#updates) |
| `append json "B7" into $o.items` | add to array [ex](jsoniq.md#updates) |
| `delete json $o.coupon` | remove a key [ex](jsoniq.md#updates) |
| `replace value of json $o.status with "shipped"` | change a value [ex](jsoniq.md#updates) |
| `rename json $o.customer as "client"` | rename a key [ex](jsoniq.md#updates) |

## JSON in XQuery (.xq)

|  |  |
|----|----|
| `$o("name")` | key lookup [ex](jsoniq.md#xqsyntax) |
| `$a(2)` | array member [ex](jsoniq.md#xqsyntax) |
| `$a()` | all members [ex](jsoniq.md#xqsyntax) |
| `jn:null()` | null [ex](jsoniq.md#xqsyntax) |
| `replace value of json $o("a") with 2` | update [ex](jsoniq.md#xqsyntax) |

## Zorba

|  |  |
|----|----|
| `zorba -i -r -f -q query.xq` | run a file, indented [ex](zorba.md#cli) |
| `-e name:=Rodolfo` | external variable [ex](zorba.md#external) |
| `variable $i := 1; while ($i le 5) { $i := $i + 1; }` | scripting [ex](zorba.md#scripting) |
| `ddl:create($name); dml:insert-last($name, $items);` | dynamic collection [ex](zorba.md#collections) |
| `dml:delete(dml:collection($name)[...])` | remove from collection [ex](zorba.md#collections) |
| `declare collection shop:products as element(product)*;` | static collection [ex](zorba.md#indexes) |
| `idml:probe-index-point-value($idx, "tea")` | index lookup [ex](zorba.md#indexes) |
