count $id
return <country id='{ $id }' name='{ $name }'>{ $city/name }</country>
</syntaxhighlightpre>
==group by==
order by $income
return element { $income } { count($ppl) }
</syntaxhighlightpre>
This query is a rewrite of [https://www.ins.cwi.nl/projects/xmark/Assets/xmlquery.txt Query #20] contained in the [https://projects.cwi.nl/xmark/ XMark Benchmark Suite] to use <code>group by</code>.
<preferred>314</preferred>
<standard>7778</standard>
</syntaxhighlightpre>
In contrast to the relational GROUP BY statement, the XQuery counterpart concatenates the values of all non-grouping variables that belong to a specific group. In the context of our example, all nodes in <code>//people/person</code> that belong to the <code>preferred</code> partition are concatenated in <code class="brush:xquery">$ppl</code> after grouping has finished. You can see this effect by changing the return statement to:
...
return element { $income } { $ppl }
</syntaxhighlightpre>
'''Result:'''
…
</challenge>
</syntaxhighlightpre>
Moreover, a value can be assigned to the grouping variable. This is shown in the following example:
return element name { data($name) }
}
</syntaxhighlightpre>
'''Result:'''
<name>Johann</name>
</persons>
</syntaxhighlightpre>
==count==
count $c
return <number count="{ $c }" number="{ $n }"/>
</syntaxhighlightpre>
==allowing empty==
for $n allowing empty in ()
return 'empty? ' || empty($n)
</syntaxhighlightpre>
==window==
only end at $e when $e - $s eq 2
return <window>{ $w }</window>
</syntaxhighlightpre>
More information on window clauses, and all other enhancements, can be found in the [https://www.w3.org/TR/xquery-30/#id-windows specification].
<li>Declaring a new ''inline function'':
<pre lang='xquery'>let $f := function($x, $y) { $x + $y }
return $f(17, 25)</syntaxhighlightpre>
'''Result:''' <code>42</code>
</li>
<li>Getting the function item of an existing (built-in or user-defined) XQuery function. The arity (number of arguments) has to be specified as there can be more than one function with the same name:
<pre lang='xquery'>let $f := math:pow#2
return $f(5, 2)</syntaxhighlightpre>
'''Result:''' <code>25</code>
</li>
$f('foo123'),
$f('bar456')
)</syntaxhighlightpre>
'''Result:''' <code>foo bar</code>
</li>
for $i in 1 to 10
return element node { $i }
</syntaxhighlightpre>
In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.
'Error [' || $err:code || ']: ' || $err:description
}
</syntaxhighlightpre>
'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code>
case "Peach" return "pink"
default return "unknown"
</syntaxhighlightpre>
'''Result:''' <code>red green pink</code>
default
return "unknown"
</syntaxhighlightpre>
'''Result:''' <code>red red</code>
<pre lang='xquery'>
element node { namespace pref { 'http://url.org/' } }
</syntaxhighlightpre>
=String Concatenations=
<pre lang='xquery'>
'Hello' || ' ' || 'Universe'
</syntaxhighlightpre>
=External Variables=
declare variable $user external := "admin";
"User:", $user
</syntaxhighlightpre>
=Serialization=
declare option output:method "xhtml";
<html/>
</syntaxhighlightpre>
'''Result:''' <code><?xml version="1.0" encoding="UTF-8"?><html></html></code>
for $t in .//text()
return string-length($t)
</syntaxhighlightpre>
'''Result:''' <code>5 5</code>
local:max(2, 3)
</syntaxhighlightpre>
=Functions=