<syntaxhighlight lang="xquery">
[], (: empty array :)
[ (1, 2) ], (: array with single member :)
<syntaxhighlight lang="xquery">
array { }, (: empty array; same as: array { () } :)
array { (1, 2) }, (: array with two members; same as: array { 1, 2 } :)
<syntaxhighlight lang="xquery">
let $array := array { 48 to 52 }
for $i in 1 to array:size($array)
<syntaxhighlight lang="xquery">
fn:data([1 to 2]) (: returns the sequence 1, 2 :)
[ 'a', 'b', 'c' ] = 'b' (: returns true :)
<syntaxhighlight lang="xquery">
let $f := function($x as xs:integer*) { count($x) }
return $f([1 to 5])
<syntaxhighlight lang="xquery">
let $f := function($x as item()*) { count($x) }
return $f([1 to 5])
<syntaxhighlight lang="xquery">
let $map := map { 'R': 'red', 'G': 'green', 'B': 'blue' }
return (
<syntaxhighlight lang="xquery">
let $maps := (
map { 'name': 'Guðrún', 'city': 'Reykjavík' },
<syntaxhighlight lang="xquery">
(: Returns 3 :)
count(('A', 'B', 'C')),
<syntaxhighlight lang="xquery">
(: Returns "This is a 'new' & 'flexible' syntax." :)
``["This is a 'new' & 'flexible' syntax."]``
<syntaxhighlight lang="xquery">
(: Returns »Count 1 2 3, and I will be there.« :)
let $c := 1 to 3
<syntaxhighlight lang="xquery">
declare option output:method 'adaptive';
<element id='id0'/>/@id,
<syntaxhighlight lang="xquery">
declare option output:method 'json';
map { "key": "value" }
<syntaxhighlight lang="xquery">
declare option output:method 'json';
<json type='object'>
===fn:parse-json===
; Signatures* <codepre>fn:parse-json( $input json as xs:string) as item()?</code>* <code>fn:parse-json($input as xs:string, $options as map(*) := ()) as item()?</codepre>
Parses the supplied string as JSON text and returns its item representation. The result may be a map, an array, a string, a double, a boolean, or an empty sequence. The allowed options can be looked up in the [https://www.w3.org/TR/xpath-functions-31/#func-parse-json specification].
<syntaxhighlight lang="xquery">
parse-json('{ "name": "john" }') (: yields { "name": "json" } :),
parse-json('[ 1, 2, 4, 8, 16]') (: yields [ 1, 2, 4, 8, 16 ] :)
===fn:json-doc===
; Signatures* <codepre>fn:json-doc( $uri href as xs:string) as item()?</code>* <code>fn:json-doc($uri as xs:string, $options as map(*) := ()) as item()?</codepre>
Retrieves the text from the specified URI, parses the supplied string as JSON text and returns its item representation (see {{Function||fn:parse-json}} for more details).
<syntaxhighlight lang="xquery">
json-doc("http://ip.jsontest.com/")('ip') (: returns your IP address :)
</syntaxhighlight>
===fn:json-to-xml===
; Signatures* <codepre>fn:json-to-xml( $string json as xs:string? $options as map(*) := ()) as document-node()?</codepre>
Converts a JSON string to an XML node representation. The allowed options can be looked up in the [https://www.w3.org/TR/xpath-functions-31/#func-json-to-xm specification].
<syntaxhighlight lang="xquery">
json-to-xml('{ "message": "world" }')
===fn:xml-to-json===
; Signatures* <codepre>fn:xml-to-json( $node as nodexs:string? $options as map(*) := ()?) as xs:string?</codepre>
Converts an XML node, whose format conforms to the results created by {{Function||fn:json-to-xml}}, to a JSON string representation. The allowed options can be looked up in the [https://www.w3.org/TR/xpath-functions-31/#func-xml-to-json specification].
<syntaxhighlight lang="xquery">
(: returns "JSON" :)
xml-to-json(<string xmlns="http://www.w3.org/2005/xpath-functions">JSON</string>)
==fn:sort==
; Signatures* <codepre>fn:sort($input as item()*) as item()*</code>* <code>fn:sort( $input as item()*, $collation as xs:string?) as xs :anyAtomicType*)) as item()*</code>* <code>= fn:sort($input as itemdefault-collation()*, $collation as xs:string?, $key as function(item()*) as xs:anyAtomicType*) := fn:data#1) as item()*</codepre>
Returns a new sequence with sorted {{Code|$input}} items, using an optional {{Code|$collation}}. If a {{Code|$key}} function is supplied, it will be applied on all items. The items of the resulting values will be sorted using the semantics of the {{Code|lt}} expression.
<syntaxhighlight lang="xquery">
sort(reverse(1 to 3)) (: yields 1, 2, 3 :),
reverse(sort(1 to 3)) (: returns the sorted order in descending order :),
==fn:contains-token==
; Signatures* <codepre>fn:contains-token( $input value as xs:string*, $token as string) as xs:boolean</code>* <code>fn:contains-token($input as xs:string*, $token as string, $collation as xs:string? := fn:default-collation()) as xs:boolean</codepre>
The supplied strings will be tokenized at whitespace boundaries. The function returns {{Code|true}} if one of the strings equals the supplied token, possibly under the rules of a supplied collation:
<syntaxhighlight lang="xquery">
contains-token(('a', 'b c', 'd'), 'c') (: yields true :)
<xml class='one two'/>/contains-token(@class, 'one') (: yields true :)
==fn:parse-ietf-date==
; Signature* <codepre>fn:parse-ietf-date( $input value as xs:string?) as xs:stringdateTime?</codepre>
Parses a string in the IETF format (which is widely used on the Internet) and returns a {{Code|xs:dateTime}} item:
<syntaxhighlight lang="xquery">
fn:parse-ietf-date('28-Feb-1984 07:07:07')" (: yields 1984-02-28T07:07:07Z :),
fn:parse-ietf-date('Wed, 01 Jun 2001 23:45:54 +02:00')" (: yields 2001-06-01T23:45:54+02:00 :)
==fn:apply==
; Signatures* <codepre>fn:apply( $function as function(*), $arguments as array(*)) as item()*</codepre>
The supplied {{Code|$function}} is invoked with the specified {{Code|$arguments}}. The arity of the function must be the same as the size of the array.
<syntaxhighlight lang="xquery">
fn:apply(concat#5, array { 1 to 5 }) (: 12345 :)
fn:apply(function($a) { sum($a) }, [ 1 to 5 ]) (: 15 :)
==fn:random-number-generator==
; Signatures* <codepre>fn:random-number-generator() as map(xs:string, item())</code>* <code>fn:random-number-generator( $seed as xs:anyAtomicType? := ()) as map(xs:string, item())</codepre>
Creates a random number generator, using an optional seed. The returned map contains three entries:
<syntaxhighlight lang="xquery">
let $rng := fn:random-number-generator()
let $number := $rng('number') (: returns a random number :)
<syntaxhighlight lang="xquery">
format-number(1984.42, '00.0e0') (: yields 19.8e2 :)
</syntaxhighlight>
<syntaxhighlight lang="xquery">
fn:tokenize(" a b c d") (: yields "a", "b", "c", "d" :)
</syntaxhighlight>
<syntaxhighlight lang="xquery">
fn:trace(<xml/>, "Node: ")/node() (: yields the debugging output "Node: <xml/>" :),
fn:trace(<xml/>)/node() (: returns the debugging output "<xml/>" :)
<syntaxhighlight lang="xquery">
fn:string-join(1 to 3) (: yields the string "123" :)
</syntaxhighlight>
<syntaxhighlight lang="xquery">
xs:hexBinary('') < xs:hexBinary('bb'),
xs:hexBinary('aa') < xs:hexBinary('bb'),
<syntaxhighlight lang="xquery">
declare default collation 'http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive';
'HTML' = 'html'
<syntaxhighlight lang="xquery">
(: returns 0 (both strings are compared as equal) :)
compare('a-b', 'ab', 'http://www.w3.org/2013/collation/UCA?alternate=shifted')
<syntaxhighlight lang="xquery">
declare function local:x() { () };
try { () } catch * { () },
<syntaxhighlight lang="xquery">
declare function local:x() { };
try { } catch * { },