Changes

Jump to navigation Jump to search
228 bytes removed ,  18:33, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
(: 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.
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>:
<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:
if($b) then '$a and $b is true' else 'only $a is true'
)
</syntaxhighlightpre>
=Functions=
(: yields "!Hi! !there!" :)
replace('Hi there', '\b', '!', 'j')
</syntaxhighlightpre>
=Serialization=
declare option db:catalog 'etc/w3-catalog.xml';
doc('doc.xml')
</syntaxhighlightpre>
==XQuery Locks==
1 to 100000000
})
</syntaxhighlightpre>
This pragma can be helpful when debugging your code.
file:write('config.xml', <config/>)
}
</syntaxhighlightpre>
==Database Pragmas==
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:
}
)
</syntaxhighlightpre>
* An XML catalog can be specified for URI rewritings. See the [[Catalog Resolver]] section for an example.
for $i in 1 to 3
return local:square($i)
</syntaxhighlightpre>
'''Query after function inlining:'''
let $a := $i
return $a * $a
</syntaxhighlightpre>
'''Query after further optimizations:'''
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.
declare %basex:inline(0) function local:e() { error() };
local:e()
</syntaxhighlightpre>
'''Result:'''
Stack Trace:
- query.xq, 2/9
</syntaxhighlightpre>
==Lazy Evaluation==
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.
<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:
<pre lang='xquery'>
'Hello World!'
</syntaxhighlightpre>
* Library modules start with a module namespace declaration and have no query body:
'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.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu