Changes

Jump to navigation Jump to search
6,085 bytes added ,  12:03, 6 May 2019
This article is part of the [[XQuery|XQuery Portal]]. It lists extensions and optimizations that are specific to the BaseX XQuery processor.
=SuffixesExpressionsSome of the extensions that have been added to BaseX may also be made available in other XQuery processors in the near future.  ==Ternary If== The [https://en.wikipedia.org/wiki/%3F: ternary if] operator provides a short syntax for conditions. It is also called '''conditional operator''' or '''ternary operator'''. In most languages, the syntax is <code>a ? b : c</code>. As <code>?</code> and <code>:</code> have already been taken in XQuery, the syntax of Perl 6 is used: <pre class="brush:xquery">$test ?? 'ok' !! 'fails'</pre> The expression returns <code>ok</code> if the effective boolean value of <code>$test</code> is true, and it returns <code>fails</code> otherwise. ==Elvis Operator== The Elvis operator is also available in other languages. It is sometimes called [https://en.wikipedia.org/wiki/Null_coalescing_operator null-coalescing operator]. In XQuery, the value of the first operand will be returned if it is a non-empty sequence. Otherwise, the value of the second operand will be returned. <pre class="brush:xquery">let $number := 123return ( (: if/then/else :) if (exists($number)) then $number else 0, (: elvis operator :) $number ?: 0)</pre> The behavior of the operator is equivalent to the {{Function|Utility|util:or}} function. ==If Without Else== In XQuery 3.1, both branches of the <code>if</code> expression need to be specified. In many cases, only one branch is required, so the <code>else</code> branch was made optional in BaseX. If the second branch is omitted, an empty sequence will be returned if the effective boolean value of the test expression is false. Some examples:
In BaseX<pre class="brush:xquery">if (doc-available($doc)) then doc($doc), files with the suffixes {{Code|.xq}}, {{Code|.xqm}}, {{Code|.xqy}}, {{Code|.xql}}, {{Code|.xqu}} and {{Code|.xquery}} are treated as XQuery files. In XQueryif (file:exists($file)) then file:delete($file), there are main and library modulesif (permissions:valid($user)) then <html>Welcome!</html></pre>
* Main modules have an expression as query body. Here is If conditions are nested, a minimum exampletrailing else branch will be associated with the innermost <code>if</code>:
<pre class="brush:xquery">
if ($a) then if($b) then 'Hello World!$a and $b is true' else 'only $b is true'
</pre>
* Library modules start with a module namespace declaration and In general, if you have no query bodymultiple or nested if expressions, additional parentheses can improve the readibility of your code:
<pre class="brush:xquery">
module namespace hello = if ($a) then ( if($b) then '$a and $b is true' else 'only $b is true'http)</pre> The behavior of the if expression is equivalent to the {{Function|Utility|util://basexif}} function. =Functions= ==Regular Expressions== In analogy with Saxon, you can specify the flag {{Code|j}} to revert to Java’s default regex parser.org/examples/hello';For example, this allows you to use the word boundary option {{Code|\b}}, which has not been included in the XQuery grammar for regular expressions:
declare function hello'''Example:''' <pre class="brush:worldxquery">(: yields "!Hi! !there!" :) { replace('Hi there', '\b', 'Hello World!'};, 'j')
</pre>
We recommend =Serialization= * <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.* With {{Code|.xqcsv}} , you can output XML nodes as suffix CSV data (see the [[CSV Module]] for for main modulesmore details).* With {{Code|json}}, and items are output as JSON as described in the [https://www.w3.org/TR/xslt-xquery-serialization-31/#json-output official specification]. If the root node is of type {{Code|.xqmelement(json)}} , items are serialized as described for library modulesthe {{Code|direct}} format in the [[JSON Module]]. However For more information and some additional BaseX-specific parameters, see the actual module type will dynamically be detected when a file is opened and parsedarticle on [[Serialization]].
=Option Declarations=
 
==Database Options==
[[Options|Local database options]] can be set in the prolog of an XQuery main module. In the option declaration, options need to be bound to the [[Database Module]] namespace. All values will be reset after the evaluation of a query:
declare option db:chop 'false';
doc('doc.xml')
</pre>
 
==XQuery Locks==
 
If [[Transactions#XQuery_Locks|XQuery Locks]] are defined in the query prolog of a module, 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">
declare option basex:write-lock 'CONFIGLOCK';
file:write('config.xml', <config/>)
</pre>
=Pragmas=
A [[Options|local database option]] ==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 also be assigned locally via a suppressed: <pre class="brush:xquery">sum( (# basex:non-deterministic #) { 1 to 100000000})</pre> This pragmacan be helpful when debugging your code. Examples:
* 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 thrown raised if the condition yields true. Without the annotation, the error will always be raised, because the referenced document is not found.
=Serialization=XQuery Locks==
* <code>basex</code> is used as the default serialization method: nodes are serialized as XML, atomic values are serialized as stringIn analogy with option declarations and pragmas, 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.1Transactions#Adaptive SerializationXQuery_Locks|adaptiveXQuery Locks]] method.* {{Code|csv}} allows you to output XML nodes as CSV data (see the [[CSV Module]] for more details).can also set via annotations:
For more information and some additional BaseX<pre class="brush:xquery">declare %basex:write-specific parameterslock('CONFIGLOCK') function local:write() { file:write('config.xml', see the article on [[Serialization]].<config/>)};</pre>
=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.
 
=Namespaces=
 
In XQuery, some namespaces are statically bound to prefixes. The following query requires no additional namespaces declarations in the query prolog:
 
<pre class="brush:xquery">
<xml:abc xmlns:prefix='uri' local:fn='x'/>,
fn:exists(1)
</pre>
 
In BaseX, various other namespaces are predefined. Apart from the namespaces that are listed on the [[Module Library]] page, the following namespaces are statically bound:
 
{| class="wikitable sortable"
|-
! Description
! Prefix
! Namespace URI
|-
| [[#Annotations|BaseX Annotations]], [[#Pragmas|Pragmas]], …
| <code>basex</code>
| <code><nowiki>http://basex.org</nowiki></code>
|-
| [[RESTXQ#Input Options|RESTXQ: Input Options]]
| <code>input</code>
| <code><nowiki>http://basex.org/modules/input</nowiki></code>
|-
| [[Repository#EXPath_Packaging|EXPath Packages]]
| <code>pkg</code>
| <code><nowiki>http://expath.org/ns/pkg</nowiki></code>
|-
| [[XQuery Errors]]
| <code>err</code>
| <code><nowiki>http://www.w3.org/2005/xqt-errors</nowiki></code>
|-
| [[Serialization]]
| <code>output</code>
| <code><nowiki>http://www.w3.org/2010/xslt-xquery-serialization</nowiki></code>
|}
 
=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: New [[#Expressions|Expressions]]: Ternary if, elvis Operator, if without else
* 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