Changes

Jump to navigation Jump to search
2,933 bytes added ,  18:56, 9 August 2018
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=
[[Options|Local database options]] ==BaseX Pragmas== Many optimizations in BaseX will only be performed if an expression is ''deterministic'' (i. e., if it always yields the same output and does not have side effects). By flagging an expression as non-deterministic, optimizations and query rewritings can be assigned locally via pragmassuppressed:
<pre class="brush:xquery">
sum( (# dbbasex:chop false non-deterministic #) { doc('doc.xml' 1 to 100000000}) }
</pre>
Various optimizations This pragma can be disabled by marking an expression as non-deterministichelpful when debugging your code. ==Database Pragmas== All [[Options|local database options]] can be assigned via pragmas. Some examples: * Enforce query to be rewritten for index access. This can e. g. be helpful if the name of a database 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> * Temporarily disable node copying in node constructors (see {{Option|COPYNODE}} for more details). The following query will be evaluated faster, and take much less memory, than without pragma, because the database nodes will not be fully copied, but only attached to the new {{Code|xml}} parent element:
<pre class="brush:xquery">
countfile:write( 'wrapped-db-nodes.xml', (# basexdb:non-deterministic copynode false #) { 1 to 10 <xml>{ db:open('huge') }</xml> })
</pre>
=Annotations=
The following implementation-defined annotations ==basex:inline== {{Code|%basex:inline([limit])}} controls if functions will be inlined. If XQuery functions are available''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 expressions, which can be adjusted via the {{CodeOption|%basex:inline([limit])INLINELIMIT}} enforces the inlining of a functionoption.  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==basex:lazy==
{{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. An example: '''Example:'''
<pre class="brush:xquery">
declare option db%basex:inlinelimit lazy variable $january := doc('0does-not-exist');declare %basex:inline function local:idif(month-from-date(current-date($x) { ) == 1) then $x };local:idjanuary else (123)
</pre>
The query annotation ensures that an error will only be optimized to thrown if the condition yields true. Without the annotation, the error will always be raised, because the referenced document is not found. =Functions= ==Regular expressions== {{CodeMark|123Introduced with Version 9.1:}}.
* In analogy with Saxon, you can specify the flag {{Code|%basex:lazyj}} enforces to revert to Java’s default regex parser. For example, this allows you to use the lazy evaluation of a global variable. Exampleword boundary option {{Code\b}}, which has not been included in the XQuery grammar for regular expressions:
'''Example:'''
<pre class="brush:xquery">
declare %basex(:lazy variable $january yields "!Hi! !there!" := doc)fn:replace('does-not-existHi there', '\b', '!', 'j');if(month-from-date(current-date()) == 1) then $january else ()
</pre>
 
The annotation ensures that an error will only be thrown if the condition yields true. Without the annotation, the error will always be thrown, 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).
Various other extensions are described in the articles on [[Full-Text#BaseX Features|XQuery Full Text]] and [[Updates|XQuery Update]].
 
=Changelog=
 
# Version 9.1:
 
* Added: [[#Regular expressions]], {{Code|j}} flag for using Java’s default regex parser.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu