=Expressions=
Some {{Announce|Removed with Version 11}}: Elvis operator <code>?:</code>, in favor of the extensions that have been added to BaseX may also be made available in other XQuery processors in the near futurenew <code>[https://qt4cg.org/specifications/xquery-40/xquery-40.html#id-otherwise otherwise]</code> expression.
==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:
<syntaxhighlight pre lang="'xquery"'>
(: if/then/else :)
if ($ok) then 1 else 0,
(: ternary if :)
$ok ?? 1 !! 0
</syntaxhighlightpre>
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.
<syntaxhighlight lang="xquery">
(: if/then/else :)
if (exists($argument)) then $argument else 0
(: elvis operator :)
$argument ?: -1
</syntaxhighlight>
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:
<syntaxhighlight pre lang="'xquery"'>
if (doc-available($doc)) then doc($doc),
if (file:exists($file)) then file:delete($file),
if (permissions:valid($user)) then <html>Welcome!</html>
</syntaxhighlightpre>
If conditions are nested, a trailing else branch will be associated with the innermost <code>if</code>:
<syntaxhighlight pre lang="'xquery"'>
if ($a) then if($b) then '$a and $b is true' else 'only $a is true'
</syntaxhighlightpre>
In general, if you have multiple or nested if expressions, additional parentheses can improve the readibility of your code:
<syntaxhighlight pre lang="'xquery"'>
if ($a) then (
if($b) then '$a and $b is true' else 'only $a is true'
)
</syntaxhighlightpre> The behavior of the if expression is equivalent to the {{Function|Utility|util:if}} function.
=Functions=
'''Example:'''
<syntaxhighlight pre lang="'xquery"'>
(: yields "!Hi! !there!" :)
replace('Hi there', '\b', '!', 'j')
</syntaxhighlightpre>
=Serialization=
[[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:
<syntaxhighlight pre lang="'xquery"'>
declare option db:catalog 'etc/w3-catalog.xml';
doc('doc.xml')
</syntaxhighlightpre>
==XQuery Locks==
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 nondeterministic, optimizations and query rewritings can be suppressed:
<syntaxhighlight pre lang="'xquery"'>
sum( (# basex:nondeterministic #) {
1 to 100000000
})
</syntaxhighlightpre>
This pragma can be helpful when debugging your code.
In analogy with option declarations and function annotations, XQuery locks can also set via pragmas. See [[Transaction Management#Options|Transaction Management]] for details and examples.
<syntaxhighlight pre lang="'xquery"'>
(# basex:write-lock CONFIGLOCK #) {
file:write('config.xml', <config/>)
}
</syntaxhighlightpre>
==Database Pragmas==
* [[Indexes|Index access rewritings]] can be enforced. This is helpful if the name of a database is not static (see [[Indexes#Enforce Rewritings|Enforce Rewritings]] for more details):
<syntaxhighlight pre lang="'xquery"'>
(# db:enforceindex #) {
for $db in ('persons1', 'persons2', 'persons3')
return db:get($db)//name[text() = 'John']
}
</syntaxhighlightpre>
* Node copying in node constructors can be disabled (see {{Option|COPYNODE}} for more details). The following query will consume much less memory than without pragma as the database nodes will not be fully duplicated, but only attached to the {{Code|xml}} parent element:
<syntaxhighlight pre lang="'xquery"'>
file:write(
'wrapped-db-nodes.xml',
}
)
</syntaxhighlightpre>
* An XML catalog can be specified for URI rewritings. See the [[Catalog Resolver]] section for an example.
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
declare function local:square($a) { $a * $a };
for $i in 1 to 3
return local:square($i)
</syntaxhighlightpre>
'''Query after function inlining:'''
<syntaxhighlight pre lang="'xquery"'>
for $i in 1 to 3
return
let $a := $i
return $a * $a
</syntaxhighlightpre>
'''Query after further optimizations:'''
<syntaxhighlight pre lang="'xquery"'>
for $i in 1 to 3
return $i * $i
</syntaxhighlightpre>
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 {{Option|INLINELIMIT}} option.
'''Example:'''
<syntaxhighlight pre lang="'xquery"'>
(: disable function inlining; the full stack trace will be shown... :)
declare %basex:inline(0) function local:e() { error() };
local:e()
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="xml">
Stopped at query.xq, 1/53:
[FOER0000] Halted on error().
Stack Trace:
- query.xq, 2/9
</syntaxhighlightpre>
==Lazy Evaluation==
'''Example:'''
<syntaxhighlight pre lang="'xquery"'>
declare %basex:lazy variable $january := doc('does-not-exist.xml');
if(month-from-date(current-date()) = 1) then $january else ()
</syntaxhighlightpre>
The annotation ensures that an error is only raised if the condition yields true. Without the annotation, the error is always raised if the referenced document is not found.
In XQuery, some namespaces are statically bound to prefixes. The following query requires no additional namespaces declarations in the query prolog:
<syntaxhighlight pre lang="'xquery"'>
<xml:abc xmlns:prefix='uri' local:fn='x'/>,
fn:exists(1)
</syntaxhighlightpre>
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:
* Main modules have an expression as query body. Here is a minimum example:
<syntaxhighlight pre lang="'xquery"'>
'Hello World!'
</syntaxhighlightpre>
* Library modules start with a module namespace declaration and have no query body:
<syntaxhighlight pre lang="'xquery"'>
module namespace hello = 'http://basex.org/examples/hello';
'Hello World!'
};
</syntaxhighlightpre>
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.
;Version 11:
* Removed: Elvis operator <code>?:</code>, in favor of the new <code>[https://qt4cg.org/specifications/xquery-40/xquery-40.html#id-otherwise otherwise]</code> expression.
* Updated: Renamed from {{Code|non-deterministic}} to {{Code|nondeterministic}}.