Changes

Jump to navigation Jump to search
1,929 bytes added ,  19:03, 16 October 2017
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
 
=Suffixes=
 
In BaseX, files with the suffixes {{Code|.xq}}, {{Code|.xqm}}, {{Code|.xqy}}, {{Code|.xql}}, {{Code|.xqu}} and {{Code|.xquery}} are treated as XQuery files. In XQuery, there are main and library modules:
 
* Main modules have an expression as query body. Here is a minimum example:
 
<pre class="brush:xquery">
'Hello World!'
</pre>
 
* Library modules start with a module namespace declaration and have no query body:
 
<pre class="brush:xquery">
module namespace hello = 'http://basex.org/examples/hello';
 
declare function hello:world() {
'Hello World!'
};
</pre>
 
We recommend {{Code|.xq}} as suffix for for main modules, and {{Code|.xqm}} for library modules. However, the actual module type will dynamically be detected when a file is opened and parsed.
=Option Declarations=
=Pragmas=
A [[Options|Local local database optionsoption]] can also be assigned locally via pragmasa pragma. Examples: * Whitespace chopping is disabled for a particular document (see {{Option|CHOP}}):
<pre class="brush:xquery">
(# db:chop false #) { doc('doc.xml') }
</pre>
 
* {{Version|9.0}}: Enforce index rewriting if database name is not static (see [[Indexes#Enforce Rewritings|Enforce Rewritings]] for more examples):
 
<pre class="brush:xquery">
(# db:enforceindex #) {
for $db in ('persons1', 'persons2', 'persons3')
return db:open($db)//name[text() = 'John']
}
</pre>
 
Many optimizations and query rewritings can be disabled by marking an expression as non-deterministic:
 
<pre class="brush:xquery">
count( (# basex:non-deterministic #) { 1 to 10 })
</pre>
=Annotations=
The following implementation-defined annotations are available==basex:inline==
* {{Code|%basex:inline([limit])}} enforces controls if functions will be inlined. If XQuery functions are ''inlined'', the function call will be replaced by a FLWOR expression, in which the function variables are bound to let clauses, and in which the function body is returned. This optimization triggers further query rewritings that will speed up your query. An example: '''Query:''' <pre class="brush:xquery">declare function local:square($a) { $a * $a };for $i in 1 to 3return local:square($i)</pre> '''Query after function inlining :''' <pre class="brush:xquery">for $i in 1 to 3return let $a := $i return $a * $a</pre> '''Query after further optimizations:''' <pre class="brush:xquery">for $i in 1 to 3return $i * $i</pre> By default, XQuery functions will be ''inlined'' if the query body is not too large and does not exceed a fixed number of a functionexpressions, which can be adjusted via the {{Option|INLINELIMIT}} option.  The annotation can be used to overwrite this global limit: Function inlining limit can be enforced if no argument is specified as argument. Inlining can will be disabled by specifying if {{Code|0}}is specified.
'''Example:'''
<pre class="brush:xquery">
(: disable function inlining; the full stack trace will be shown... :)
declare %basex:inline(0) function local:e() { error() };
local:e()
</pre>
In the next example, function inlining was disabled globally by assigning {{Code|0}} to the [[Options#INLINELIMIT|INLINELIMIT]] option. However, annotation is enforced to a single function: '''Example:'''<pre class="brush:xquery">declare option db:inlinelimit '0';declare %=basex:inline function local:id($x) { $x };local:id(123)</pre> The query will be optimized to {{Code|123}}. lazy==
* {{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. ExampleAn example:
'''Example:'''
</pre>
The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrownraised, because the referenced document is not found.
=Serialization=
* Since {{Version|8.4}}, <code>basex</code> is used as the default serialization method: nodes are serialized as XML, atomic values are serialized as string, and items of binary type are output in their native byte representation. Function items (including maps and arrays) are output just like with the [[XQuery 3.1#Adaptive Serialization|adaptive]] method.
* {{Code|csv}} allows you to output XML nodes as CSV data (see the [[CSV Module]] for more details).
=Miscellaneous=
Various other extensions are described in the articles on [[Full-Text#BaseX Features|XQuery Full Text]] and [[Updates#Concepts|XQuery Update]].
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu