Difference between revisions of "XQuery 3.0"

From BaseX Documentation
Jump to navigation Jump to search
Line 6: Line 6:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $country in db:open('factbook')//country
 
for $country in db:open('factbook')//country
 
where $country/@population > 100000000
 
where $country/@population > 100000000
Line 13: Line 13:
 
count $id
 
count $id
 
return <country id='{ $id }' name='{ $name }'>{ $city/name }</country>
 
return <country id='{ $id }' name='{ $name }'>{ $city/name }</country>
</pre>
+
</syntaxhighlight>
  
 
==group by==
 
==group by==
Line 20: Line 20:
  
 
'''XQuery:'''  
 
'''XQuery:'''  
<pre class="brush:xquery">  
+
<syntaxhighlight lang="xquery">
 
for $ppl in doc('xmark')//people/person   
 
for $ppl in doc('xmark')//people/person   
 
let $ic := $ppl/profile/@income
 
let $ic := $ppl/profile/@income
Line 34: Line 34:
 
order by $income
 
order by $income
 
return element { $income } { count($ppl) }
 
return element { $income } { count($ppl) }
</pre>  
+
</syntaxhighlight>  
  
 
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>.
 
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>.
Line 40: Line 40:
  
 
'''Result:'''  
 
'''Result:'''  
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<challenge>4731</challenge>
 
<challenge>4731</challenge>
 
<na>12677</na>
 
<na>12677</na>
 
<preferred>314</preferred>
 
<preferred>314</preferred>
 
<standard>7778</standard>
 
<standard>7778</standard>
</pre>
+
</syntaxhighlight>
  
 
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:
 
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 class="brush:xquery">  
+
<syntaxhighlight lang="xquery">  
 
...
 
...
 
return element { $income } { $ppl }
 
return element { $income } { $ppl }
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<challenge>
 
<challenge>
 
   <person id="person0">
 
   <person id="person0">
Line 63: Line 63:
 
     …
 
     …
 
</challenge>
 
</challenge>
</pre>
+
</syntaxhighlight>
  
 
Moreover, a value can be assigned to the grouping variable. This is shown in the following example:
 
Moreover, a value can be assigned to the grouping variable. This is shown in the following example:
  
 
'''XQuery:'''  
 
'''XQuery:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
let $data :=
 
let $data :=
 
   <xml>
 
   <xml>
Line 81: Line 81:
 
   $person/@name ! element name { data() }
 
   $person/@name ! element name { data() }
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<persons country="USA">
 
<persons country="USA">
 
   <name>John</name>
 
   <name>John</name>
Line 92: Line 92:
 
   <name>Johann</name>
 
   <name>Johann</name>
 
</persons>
 
</persons>
</pre>
+
</syntaxhighlight>
  
 
==count==
 
==count==
Line 98: Line 98:
 
The {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.
 
The {{Code|count}} clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $n in (1 to 10)[. mod 2 = 1]
 
for $n in (1 to 10)[. mod 2 = 1]
 
count $c
 
count $c
 
return &lt;number count="{ $c }" number="{ $n }"/&gt;
 
return &lt;number count="{ $c }" number="{ $n }"/&gt;
</pre>
+
</syntaxhighlight>
  
 
==allowing empty==
 
==allowing empty==
Line 108: Line 108:
 
The {{Code|allowing empty}} provides functionality similar to outer joins in SQL:
 
The {{Code|allowing empty}} provides functionality similar to outer joins in SQL:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $n allowing empty in ()
 
for $n allowing empty in ()
 
return 'empty? ' || empty($n)
 
return 'empty? ' || empty($n)
</pre>
+
</syntaxhighlight>
  
 
==window==
 
==window==
Line 117: Line 117:
 
Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example:
 
Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
 
for tumbling window $w in (2, 4, 6, 8, 10, 12, 14)
 
     start at $s when fn:true()
 
     start at $s when fn:true()
 
     only end at $e when $e - $s eq 2
 
     only end at $e when $e - $s eq 2
 
return &lt;window&gt;{ $w }&lt;/window&gt;
 
return &lt;window&gt;{ $w }&lt;/window&gt;
</pre>
+
</syntaxhighlight>
  
 
More information on window clauses, and all other enhancements, can be found in the [http://www.w3.org/TR/xquery-30/#id-windows specification].
 
More information on window clauses, and all other enhancements, can be found in the [http://www.w3.org/TR/xquery-30/#id-windows specification].
Line 136: Line 136:
 
<ul>
 
<ul>
 
<li>Declaring a new ''inline function'':
 
<li>Declaring a new ''inline function'':
<pre class="brush:xquery">let $f := function($x, $y) { $x + $y }
+
<syntaxhighlight lang="xquery">let $f := function($x, $y) { $x + $y }
return $f(17, 25)</pre>  
+
return $f(17, 25)</syntaxhighlight>  
 
'''Result:''' <code>42</code>
 
'''Result:''' <code>42</code>
 
</li>
 
</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:
 
<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 class="brush:xquery">let $f := math:pow#2
+
<syntaxhighlight lang="xquery">let $f := math:pow#2
return $f(5, 2)</pre>  
+
return $f(5, 2)</syntaxhighlight>  
 
'''Result:''' <code>25</code>
 
'''Result:''' <code>25</code>
 
</li>
 
</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.
 
<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 class="brush:xquery">let $f := fn:substring(?, 1, 3)
+
<syntaxhighlight lang="xquery">let $f := fn:substring(?, 1, 3)
 
return (
 
return (
 
   $f('foo123'),
 
   $f('foo123'),
 
   $f('bar456')
 
   $f('bar456')
)</pre>  
+
)</syntaxhighlight>  
 
'''Result:''' <code>foo bar</code>
 
'''Result:''' <code>foo bar</code>
 
</li>
 
</li>
Line 162: Line 162:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
(: Simple map notation :)
 
(: Simple map notation :)
 
(1 to 10) ! element node { . },
 
(1 to 10) ! element node { . },
Line 168: Line 168:
 
for $i in 1 to 10
 
for $i in 1 to 10
 
return element node { $i }
 
return element node { $i }
</pre>
+
</syntaxhighlight>
  
 
In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.
 
In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.
Line 177: Line 177:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
try {
 
try {
 
   1 + '2'
 
   1 + '2'
Line 185: Line 185:
 
   'Error [' || $err:code || ']: ' || $err:description
 
   'Error [' || $err:code || ']: ' || $err:description
 
}
 
}
</pre>
+
</syntaxhighlight>
 
'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code>
 
'''Result:''' <code>Typing error: '+' operator: number expected, xs:string found.</code>
  
Line 203: Line 203:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $fruit in ("Apple", "Pear", "Peach")
 
for $fruit in ("Apple", "Pear", "Peach")
 
return switch ($fruit)
 
return switch ($fruit)
Line 210: Line 210:
 
   case "Peach" return "pink"
 
   case "Peach" return "pink"
 
   default      return "unknown"
 
   default      return "unknown"
</pre>  
+
</syntaxhighlight>  
 
'''Result:''' <code>red green pink</code>
 
'''Result:''' <code>red green pink</code>
  
Line 216: Line 216:
  
 
'''Example:'''
 
'''Example:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $fruit in ("Apple", "Cherry")
 
for $fruit in ("Apple", "Cherry")
 
return switch ($fruit)
 
return switch ($fruit)
Line 228: Line 228:
 
   default
 
   default
 
     return "unknown"
 
     return "unknown"
</pre>
+
</syntaxhighlight>
 
'''Result:''' <code>red red</code>
 
'''Result:''' <code>red red</code>
  
Line 243: Line 243:
 
New namespaces can be created via so-called 'Computed Namespace Constructors'.
 
New namespaces can be created via so-called 'Computed Namespace Constructors'.
  
<pre class="brush:xquery">  
+
<syntaxhighlight lang="xquery">  
 
element node { namespace pref { 'http://url.org/' } }
 
element node { namespace pref { 'http://url.org/' } }
</pre>
+
</syntaxhighlight>
  
 
=String Concatenations=
 
=String Concatenations=
Line 251: Line 251:
 
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.
 
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 class="brush:xquery">  
+
<syntaxhighlight lang="xquery">  
 
'Hello' || ' ' || 'Universe'
 
'Hello' || ' ' || 'Universe'
</pre>
+
</syntaxhighlight>
  
 
=External Variables=
 
=External Variables=
Line 259: Line 259:
 
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.
 
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 class="brush:xquery">  
+
<syntaxhighlight lang="xquery">  
 
declare variable $user external := "admin";
 
declare variable $user external := "admin";
 
"User:", $user
 
"User:", $user
</pre>
+
</syntaxhighlight>
  
 
=Serialization=
 
=Serialization=
Line 269: Line 269:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
 
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
 
declare option output:omit-xml-declaration "no";
 
declare option output:omit-xml-declaration "no";
 
declare option output:method "xhtml";
 
declare option output:method "xhtml";
 
&lt;html/&gt;
 
&lt;html/&gt;
</pre>  
+
</syntaxhighlight>  
 
'''Result:''' <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html&gt;&lt;/html&gt;</code>
 
'''Result:''' <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;html&gt;&lt;/html&gt;</code>
  
Line 284: Line 284:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
declare context item := document {
 
declare context item := document {
 
   <xml>
 
   <xml>
Line 294: Line 294:
 
for $t in .//text()
 
for $t in .//text()
 
return string-length($t)
 
return string-length($t)
</pre>  
+
</syntaxhighlight>  
 
'''Result:''' <code>5 5</code>
 
'''Result:''' <code>5 5</code>
  
Line 302: Line 302:
  
 
'''Example:'''  
 
'''Example:'''  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
declare %private function local:max($x1, $x2) {
 
declare %private function local:max($x1, $x2) {
 
   if($x1 > $x2) then $x1 else $x2
 
   if($x1 > $x2) then $x1 else $x2
Line 308: Line 308:
  
 
local:max(2, 3)
 
local:max(2, 3)
</pre>
+
</syntaxhighlight>
  
 
=Functions=
 
=Functions=

Revision as of 13:47, 27 February 2020

This article is part of the XQuery Portal. It provides a summary of the most important features of the XQuery 3.0 Recommendation.

Enhanced FLWOR Expressions

Most clauses of FLWOR expressions can be specified in an arbitrary order: additional let and for clauses can be put after a where clause, and multiple where, order by and group by statements can be used. This means that many nested loops can now be rewritten to a single FLWOR expression.

Example: <syntaxhighlight lang="xquery"> for $country in db:open('factbook')//country where $country/@population > 100000000 for $city in $country//city[population > 1000000] group by $name := $country/name[1] count $id return <country id='{ $id }' name='{ $name }'>{ $city/name }</country> </syntaxhighlight>

group by

FLWOR expressions have been extended to include the group by clause, which is well-established in SQL. group by can be used to apply value-based partitioning to query results:

XQuery: <syntaxhighlight lang="xquery"> for $ppl in doc('xmark')//people/person let $ic := $ppl/profile/@income let $income := if($ic < 30000) then

                  "challenge" 
               else if($ic >= 30000 and $ic < 100000) then 
                  "standard" 
               else if($ic >= 100000) then 
                  "preferred" 
               else 
                  "na"  

group by $income order by $income return element { $income } { count($ppl) } </syntaxhighlight>

This query is a rewrite of Query #20 contained in the XMark Benchmark Suite to use group by. The query partitions the customers based on their income.

Result: <syntaxhighlight lang="xml"> <challenge>4731</challenge> <na>12677</na> <preferred>314</preferred> <standard>7778</standard> </syntaxhighlight>

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 //people/person that belong to the preferred partition are concatenated in $ppl after grouping has finished. You can see this effect by changing the return statement to:

<syntaxhighlight lang="xquery"> ... return element { $income } { $ppl } </syntaxhighlight>

Result: <syntaxhighlight lang="xml"> <challenge>

 <person id="person0">
   <name>Kasidit Treweek</name>
   …
 <person id="personX">
   …

</challenge> </syntaxhighlight>

Moreover, a value can be assigned to the grouping variable. This is shown in the following example:

XQuery: <syntaxhighlight lang="xquery"> let $data :=

 <xml>
   <person country='USA' name='John'/>
   <person country='USA' name='Jack'/>
   <person country='Germany' name='Johann'/>
 </xml>

for $person in $data/person group by $country := $person/@country/string() return element persons {

 attribute country { $country },
 $person/@name ! element name { data() }

} </syntaxhighlight>

Result: <syntaxhighlight lang="xml"> <persons country="USA">

 <name>John</name>
 <name>Jack</name>

</persons> <persons country="Germany">

 <name>Johann</name>

</persons> </syntaxhighlight>

count

The count clause enhances the FLWOR expression with a variable that enumerates the iterated tuples.

<syntaxhighlight lang="xquery"> for $n in (1 to 10)[. mod 2 = 1] count $c return <number count="{ $c }" number="{ $n }"/> </syntaxhighlight>

allowing empty

The allowing empty provides functionality similar to outer joins in SQL:

<syntaxhighlight lang="xquery"> for $n allowing empty in () return 'empty? ' || empty($n) </syntaxhighlight>

window

Window clauses provide a rich set of variable declarations to process sub-sequences of iterated tuples. An example:

<syntaxhighlight lang="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 <window>{ $w }</window> </syntaxhighlight>

More information on window clauses, and all other enhancements, can be found in the specification.

Function Items

One of the most distinguishing features added in XQuery 3.0 are function items, also known as lambdas or lambda functions. They make it possible to abstract over functions and thus write more modular code.

Examples:

Function items can be obtained in three different ways:

  • Declaring a new inline function: <syntaxhighlight lang="xquery">let $f := function($x, $y) { $x + $y } return $f(17, 25)</syntaxhighlight> Result: 42
  • 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: <syntaxhighlight lang="xquery">let $f := math:pow#2 return $f(5, 2)</syntaxhighlight> Result: 25
  • Partially applying another function or function item. This is done by supplying only some of the required arguments, writing the placeholder ? in the positions of the arguments left out. The produced function item has one argument for every placeholder. <syntaxhighlight lang="xquery">let $f := fn:substring(?, 1, 3) return ( $f('foo123'), $f('bar456') )</syntaxhighlight> Result: foo bar

Function items can also be passed as arguments to and returned as results from functions. These so-called Higher-Order Functions like fn:map and fn:fold-left are discussed in more depth on their own Wiki page.

Simple Map Operator

The simple map operator ! provides a compact notation for applying the results of a first to a second expression: the resulting items of the first expression are bound to the context item one by one, and the second expression is evaluated for each item. The map operator may be used as replacement for FLWOR expressions:

Example: <syntaxhighlight lang="xquery"> (: Simple map notation :) (1 to 10) ! element node { . }, (: FLWOR notation :) for $i in 1 to 10 return element node { $i } </syntaxhighlight>

In contrast to path expressions, the results of the map operator will not be made duplicate-free and returned in document order.

Try/Catch

The try/catch construct can be used to handle errors at runtime:

Example: <syntaxhighlight lang="xquery"> try {

 1 + '2'

} catch err:XPTY0004 {

 'Typing error: ' || $err:description

} catch * {

 'Error [' || $err:code || ']: ' || $err:description

} </syntaxhighlight> Result: Typing error: '+' operator: number expected, xs:string found.

Within the scope of the catch clause, a number of variables are implicitly declared, giving information about the error that occurred:

  • $err:code error code
  • $err:description: error message
  • $err:value: value associated with the error (optional)
  • $err:module: URI of the module where the error occurred
  • $err:line-number: line number where the error occurred
  • $err:column-number: column number where the error occurred
  • $err:additional: error stack trace

Switch

The switch statement is available in many other programming languages. It chooses one of several expressions to evaluate based on its input value.

Example: <syntaxhighlight lang="xquery"> for $fruit in ("Apple", "Pear", "Peach") return switch ($fruit)

 case "Apple" return "red"
 case "Pear"  return "green"
 case "Peach" return "pink"
 default      return "unknown"

</syntaxhighlight> Result: red green pink

The expression to evaluate can correspond to multiple input values.

Example: <syntaxhighlight lang="xquery"> for $fruit in ("Apple", "Cherry") return switch ($fruit)

 case "Apple"
 case "Cherry"
    return "red"
 case "Pear"
    return "green"
 case "Peach"
    return "pink"
 default
    return "unknown"

</syntaxhighlight> Result: red red

Expanded QNames

A QName can be prefixed with the letter "Q" and a namespace URI in the Clark Notation.

Examples:

  • Q{http://www.w3.org/2005/xpath-functions/math}pi() returns the number π
  • Q{java:java.io.FileOutputStream}new("output.txt") creates a new Java file output stream

Namespace Constructors

New namespaces can be created via so-called 'Computed Namespace Constructors'.

<syntaxhighlight lang="xquery"> element node { namespace pref { 'http://url.org/' } } </syntaxhighlight>

String Concatenations

Two vertical bars || (also named pipe characters) can be used to concatenate strings. This operator is a shortcut for the fn:concat() function.

<syntaxhighlight lang="xquery"> 'Hello' || ' ' || 'Universe' </syntaxhighlight>

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.

<syntaxhighlight lang="xquery"> declare variable $user external := "admin"; "User:", $user </syntaxhighlight>

Serialization

Serialization parameters can be defined within XQuery expressions. Parameters are placed in the query prolog and need to be specified as option declarations, using the output prefix.

Example: <syntaxhighlight lang="xquery"> declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization"; declare option output:omit-xml-declaration "no"; declare option output:method "xhtml"; <html/> </syntaxhighlight> Result: <?xml version="1.0" encoding="UTF-8"?><html></html>

In BaseX, the output prefix is statically bound and can thus be omitted. Note that all namespaces need to be specified when using external APIs, such as XQJ.

Context Item

The context item can be specified in the prolog of an XQuery expression:

Example: <syntaxhighlight lang="xquery"> declare context item := document {

 <xml>
   <text>Hello</text>
   <text>World</text>
 </xml>

};

for $t in .//text() return string-length($t) </syntaxhighlight> Result: 5 5

Annotations

XQuery 3.0 introduces annotations to declare properties associated with functions and variables. For instance, a function may be declared %public, %private, or %updating.

Example: <syntaxhighlight lang="xquery"> declare %private function local:max($x1, $x2) {

 if($x1 > $x2) then $x1 else $x2

};

local:max(2, 3) </syntaxhighlight>

Functions

The following functions have been added in the XQuery 3.0 Functions and Operators Specification:

fn:analyze-string* fn:available-environment-variables, fn:element-with-id, fn:environment-variable, fn:filter, fn:fold-left, fn:fold-right, fn:for-each, fn:for-each-pair, fn:format-date, fn:format-dateTime, fn:format-integer, fn:format-number, fn:format-time, fn:function-arity, fn:function-lookup, fn:function-name, fn:generate-id, fn:has-children, fn:head, fn:innermost, fn:outermost, fn:parse-xml, fn:parse-xml-fragment, fn:path, fn:serialize, fn:tail, fn:unparsed-text, fn:unparsed-text-available, fn:unparsed-text-lines, fn:uri-collection

New signatures have been added for the following functions:

fn:document-uri, fn:string-join, fn:node-name, fn:round, fn:data

Changelog

Version 8.4
  • Added: %non-deterministic
Version 8.0
  • Added: %basex:inline, %basex:lazy
Version 7.7
Version 7.3
Version 7.2
Version 7.1
Version 7.0