Changes

Jump to navigation Jump to search
390 bytes added ,  14:47, 27 February 2020
no edit summary
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $country in db:open('factbook')//country
where $country/@population > 100000000
count $id
return <country id='{ $id }' name='{ $name }'>{ $city/name }</country>
</presyntaxhighlight>
==group by==
'''XQuery:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $ppl in doc('xmark')//people/person
let $ic := $ppl/profile/@income
order by $income
return element { $income } { count($ppl) }
</presyntaxhighlight>
This query is a rewrite of [http://www.ins.cwi.nl/projects/xmark/Assets/xmlquery.txt Query #20] contained in the [http://www.ins.cwi.nl/projects/xmark XMark Benchmark Suite] to use <code>group by</code>.
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<challenge>4731</challenge>
<na>12677</na>
<preferred>314</preferred>
<standard>7778</standard>
</presyntaxhighlight>
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:
<pre classsyntaxhighlight lang="brush:xquery">
...
return element { $income } { $ppl }
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<challenge>
<person id="person0">
</challenge>
</presyntaxhighlight>
Moreover, a value can be assigned to the grouping variable. This is shown in the following example:
'''XQuery:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $data :=
<xml>
$person/@name ! element name { data() }
}
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<persons country="USA">
<name>John</name>
<name>Johann</name>
</persons>
</presyntaxhighlight>
==count==
The {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.
<pre classsyntaxhighlight lang="brush:xquery">
for $n in (1 to 10)[. mod 2 = 1]
count $c
return &lt;number count="{ $c }" number="{ $n }"/&gt;
</presyntaxhighlight>
==allowing empty==
The {{Code|allowing empty}} provides functionality similar to outer joins in SQL:
<pre classsyntaxhighlight lang="brush:xquery">
for $n allowing empty in ()
return 'empty? ' || empty($n)
</presyntaxhighlight>
==window==
Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example:
<pre classsyntaxhighlight lang="brush:xquery">
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
start at $s when fn:true()
only end at $e when $e - $s eq 2
return &lt;window&gt;{ $w }&lt;/window&gt;
</presyntaxhighlight>
More information on window clauses, and all other enhancements, can be found in the [http://www.w3.org/TR/xquery-30/#id-windows specification].
<ul>
<li>Declaring a new ''inline function'':
<pre classsyntaxhighlight lang="brush:xquery">let $f := function($x, $y) { $x + $y }return $f(17, 25)</presyntaxhighlight>
'''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 classsyntaxhighlight lang="brush:xquery">let $f := math:pow#2return $f(5, 2)</presyntaxhighlight>
'''Result:''' <code>25</code>
</li>
<li>''Partially applying'' another function or function item. This is done by supplying only some of the required arguments, writing the placeholder <code>?</code> in the positions of the arguments left out. The produced function item has one argument for every placeholder.
<pre classsyntaxhighlight lang="brush:xquery">let $f := fn:substring(?, 1, 3)
return (
$f('foo123'),
$f('bar456')
)</presyntaxhighlight>
'''Result:''' <code>foo bar</code>
</li>
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
(: Simple map notation :)
(1 to 10) ! element node { . },
for $i in 1 to 10
return element node { $i }
</presyntaxhighlight>
In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
try {
1 + '2'
'Error [' || $err:code || ']: ' || $err:description
}
</presyntaxhighlight>
'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code>
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $fruit in ("Apple", "Pear", "Peach")
return switch ($fruit)
case "Peach" return "pink"
default return "unknown"
</presyntaxhighlight>
'''Result:''' <code>red green pink</code>
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $fruit in ("Apple", "Cherry")
return switch ($fruit)
default
return "unknown"
</presyntaxhighlight>
'''Result:''' <code>red red</code>
New namespaces can be created via so-called 'Computed Namespace Constructors'.
<pre classsyntaxhighlight lang="brush:xquery">
element node { namespace pref { 'http://url.org/' } }
</presyntaxhighlight>
=String Concatenations=
Two vertical bars <code>||</code> (also named ''pipe characters'') can be used to concatenate strings. This operator is a shortcut for the {{Code|fn:concat()}} function.
<pre classsyntaxhighlight lang="brush:xquery">
'Hello' || ' ' || 'Universe'
</presyntaxhighlight>
=External Variables=
Default values can be attached to external variable declarations. This way, an expression can also be evaluated if its external variables have not been bound to a new value.
<pre classsyntaxhighlight lang="brush:xquery">
declare variable $user external := "admin";
"User:", $user
</presyntaxhighlight>
=Serialization=
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:omit-xml-declaration "no";
declare option output:method "xhtml";
&lt;html/&gt;
</presyntaxhighlight>
'''Result:''' <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html&gt;&lt;/html&gt;</code>
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
declare context item := document {
<xml>
for $t in .//text()
return string-length($t)
</presyntaxhighlight>
'''Result:''' <code>5 5</code>
'''Example:'''
<pre classsyntaxhighlight lang="brush:xquery">
declare %private function local:max($x1, $x2) {
if($x1 > $x2) then $x1 else $x2
local:max(2, 3)
</presyntaxhighlight>
=Functions=
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu