Changes

Jump to navigation Jump to search
2,307 bytes added ,  19:20, 20 August 2018
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
=SuffixesOption Declarations=
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:==Database Options==
* Main modules have [[Options|Local database options]] can be set in the prolog of an expression as query bodyXQuery main module. In the option declaration, options need to be bound to the [[Database Module]] namespace. Here is All values will be reset after the evaluation of a minimum examplequery:
<pre class="brush:xquery">
declare option db:chop 'Hello World!false';doc('doc.xml')
</pre>
* Library modules start with ==XQuery Locks== If [[Transactions#XQuery_Locks|XQuery Locks]] are defined in the query prolog of a module namespace declaration and have no query body, access to functions of this module locks will be controlled by the central transaction management. If the following XQuery code is called by two clients in parallel, the queries will be evaluated one after another:
<pre class="brush:xquery">
module namespace hello = declare option basex:write-lock 'http://basex.org/examples/helloCONFIGLOCK'; declare function hellofile:worldwrite() { 'Hello World!config.xml'};, <config/>)
</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.=Pragmas=
=Option Declarations=BaseX Pragmas==
[[Options|Local database options]] can Many optimizations in BaseX will only be set in the prolog of performed if an XQuery main moduleexpression is ''deterministic'' (i. e. In the option declaration, options need to be bound to if it always yields the [[Database Module]] namespacesame output and does not have side effects). All values will By flagging an expression as non-deterministic, optimizations and query rewritings can be reset after the evaluation of a querysuppressed:
<pre class="brush:xquery">
declare option dbsum( (# basex:chop 'false';non-deterministic #) { 1 to 100000000doc('doc.xml'})
</pre>
=Pragmas=This pragma can be helpful when debugging your code.
A [[Options{{Mark|local database option]] can also be assigned locally via a pragmaIntroduced with Version 9. Examples1:}}
* Whitespace chopping is disabled for a particular document (see {{OptionIn analogy with option declarations and function annotations, [[Transactions#XQuery_Locks|CHOP}})XQuery Locks]] can also set via pragmas:
<pre class="brush:xquery">
(# dbbasex:chop false write-lock CONFIGLOCK #) { doc file:write('docconfig.xml', <config/>) }
</pre>
* {{Version==Database Pragmas== All [[Options|9local options]] can be assigned via pragmas.0}}Some examples:  * Enforce query to be rewritten for index rewriting access. This can e. g. be helpful if the name of a database name is not static (see [[Indexes#Enforce Rewritings|Enforce Rewritings]] for more examples):
<pre class="brush:xquery">
</pre>
Many optimizations * Temporarily disable node copying in node constructors (see {{Option|COPYNODE}} for more details). The following query will be evaluated faster, and query rewritings can take much less memory, than without pragma, because the database nodes will not be disabled by marking an expression as non-deterministicfully 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=
==basex:inlineFunction Inlining==
{{Code|%basex:inline([limit])}} controls if functions will be inlined.
</pre>
==basex:lazyLazy Evaluation==
{{Code|%basex:lazy}} enforces the lazy evaluation of a global variable. An example:
'''Example:'''
<pre class="brush:xquery">
declare %basex:lazy variable $january := doc('does-not-exist');
if(month-from-date(current-date()) == 1) then $january else ()</pre> The annotation ensures that an error will only be raised if the condition yields true. Without the annotation, the error will always be raised, because the referenced document is not found. ==XQuery Locks== {{Mark|Introduced with Version 9.1:}} In analogy with option declarations and pragmas, [[Transactions#XQuery_Locks|XQuery Locks]] can also set via annotations: <pre class="brush:xquery">declare %basex:write-lock('CONFIGLOCK') function local:write() { file:write('config.xml', <config/>)};
</pre>
The annotation ensures that an error will only be thrown if =Functions= ==Regular Expressions== {{Mark|Introduced with Version 9.1:}} In analogy with Saxon, you can specify the condition yields trueflag {{Code|j}} to revert to Java’s default regex parser. Without For example, this allows you to use the annotationword boundary option {{Code|\b}}, which has not been included in the error will always be raisedXQuery grammar for regular expressions: '''Example:''' <pre class="brush:xquery">(: yields "!Hi! !there!" :)replace('Hi there', because the referenced document is not found.'\b', '!', 'j')</pre>
=Serialization=
For more information and some additional BaseX-specific parameters, see the article on [[Serialization]].
=Non-determinismDeterminism=
In [http://www.w3.org/TR/xpath-functions-31/#dt-deterministic XQuery], ''deterministic'' functions are “guaranteed to produce ·identical· results from repeated calls within a single ·execution scope· if the explicit and implicit arguments are identical”. In BaseX, many extension functions are non-deterministic or side-effecting. If an expression is internally flagged as non-deterministic, various optimizations that might change their execution order will not be applied.
Two non-deterministic functions will be bound to <code>$read</code>, and the result of the function call will be bound to <code>$ignored</code>. As the variable is not referenced in the subsequent code, the let clause would usually be discarded by the compiler. In the given query, however, execution will be enforced because of the BaseX-specific {{Code|non-deterministic}} keyword.
 
=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.
=Miscellaneous=
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: XQuery Locks via pragmas and function annotations.
* Added: [[#Regular expressions|Regular Expressions]], {{Code|j}} flag for using Java’s default regex parser.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu