25.12.2013 Views

Tamino XQuery User Guide - Software AG Documentation

Tamino XQuery User Guide - Software AG Documentation

Tamino XQuery User Guide - Software AG Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced Usage<br />

declare function local:siblings($a as element()) as element()*<br />

{<br />

for $x in $a/../*<br />

where not($x is $a)<br />

return $x<br />

}<br />

let $a := foo bar <br />

return local:siblings($a/a1)<br />

It takes an element node as argument and returns all its sibling element nodes. Following the<br />

function declaration it is directly used in a query body resulting in the node bar.<br />

<strong>User</strong>-defined functions may be recursive, that is a function can contain a call to itself:<br />

declare function local:depth($e as node()) as xs:integer<br />

{<br />

if (not($e/*))<br />

then<br />

1<br />

else<br />

max(for $c in $e/* return local:depth($c)) + 1<br />

}<br />

This function computes the depth of a node by calling itself for its children nodes.<br />

To avoid running out of memory during the execution of a user-defined function, the <strong>XQuery</strong><br />

processor restricts the amount of memory that can be used by a single query processing thread<br />

during query execution. The amount of memory can be modified by an <strong>XQuery</strong> pragma. Similarly,<br />

you can avoid a stack overflow by restricting the call stack of the function. The following pragma<br />

sets the available memory to 100 MB and the maximum call stack depth to 1000 (default value is<br />

256):<br />

{?execution memory="100" call-stack-depth="1000"?}<br />

Note: See also the Performance <strong>Guide</strong> for information about optimizing user-defined functions<br />

using inlining techniques.<br />

Defining and Using Modules<br />

In <strong>XQuery</strong>, code can be organized into modules. There are two principal kinds of modules: a main<br />

module that contains the query body, and library modules that you can import into other library<br />

modules or into the main module. For example, you can put the functions defined in the last section<br />

into a module of its own:<br />

78<br />

<strong>XQuery</strong> <strong>User</strong> <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!