View as source file or XML.
OptionDecl ::= "declare" "option" QName StringLiteralwhere QName is the name of the option and StringLiteral is the value that is assigned to this name.For example, the following declaration disables scripting support in the module that contains the option declaration.
declare namespace op = "http://zorba.io/options/features"; declare option op:disable "scripting";
declare namespace op = "http://zorba.io/options/features"; declare namespace f = "http://zorba.io/features"; declare option op:disable "f:scripting, f:ddl";If a given feature doesn't specify a prefix, the name is resolved against the default feature namespace . All Zorba specific features presented in the following are declared in this namespace.
<country>
tag whose name attribute gives the name of the associated country. The optimizer can use the for-serialization-only hint to avoid copying the $sale nodes when it constructs each of the new <country>
nodes.declare namespace opt = "http://zorba.io/options/optimizer"; declare option opt:enable "for-serialization-only"; for $sale in doc("sales.xml")/sale let $country := $sale/country group by $country return <country name={$country}>{$sale}</country>The for-serialization-only hint can also be turned on or off via the use of the Zorba_CompilerHints struct in the C++ API. Using an option declaration to enable or disable the hint overwrites the value stored in Zorba_CompilerHints.
declare namespace op = "http://zorba.io/options/warnings"; declare namespace w = "http://zorba.io/warnings"; declare option op:error "w:all";If a given warning doesn't specify a prefix, the name is resolved against the default warning namespace . All Zorba specific warnings are defined in this namespace. A comprehensive list of warnings is contained at the end of the C++ header file diagnostic_list.h.The following snippet causes the warning ZWST0002 (warn for unknown annotations) to be suppressed:
declare namespace op = "http://zorba.io/options/warnings"; declare option op:disable "ZWST0002";
declare namespace ver = "http://zorba.io/options/versioning"; declare option ver:module-version "2.5"; declare option ver:zorba-version "2.0";
declare function sctx:option($name as xs:QName) as xs:string? external;
zorba::StaticContext_t lContext = zorba->createStaticContext(); zorba::Item lEnable = zorba->getItemFactory()->createQName( "http://zorba.io/options/features", "", "enable"); lContext->declareOption(lEnable, "hof"); zorba::XQuery_t lQuery = zorba->compileQuery("1+1", lContext);Declaring and retrieving the value of an option is particularly useful in external functions. The developer of an external function can retrieve the value of an option (from the static context passed to the function). This way, she can customize the behavior of her function.
declare %private function mymodule:foo() { "foo" };
declare namespace an = "http://zorba.io/annotations"; declare %an:nondeterministic function random:random() as xs:integer external;If not specified otherwise, all functions are being treated as deterministic. If some function invokes a nondeterministic function, the invoking function itself must also be declared as nondeterministc, otherwise zerr::ZXQP0040 is raised.
declare %an:sequential function http:post( $href as xs:string, $body as item(), $content-type as xs:string) as item()+ { ... }
declare namespace an = "http://zorba.io/annotations"; declare %an:nonassignable variable $var := 3;If a value is assigned to a nonassignable variable, the error XSST0007 in the Zorba error namespace is raised.
declare %an:variadic %an:sequential function map:create( $name as xs:QName, $key-type as xs:QName) as empty-sequence() external;
import module namespace file = "http://expath.org/ns/file"; file:read-text("big_file.txt")However, the disadvantage is that a streamable string can only be consumed exactly once. If a streamable string is consumed more than once, an error is raised. In order to enable multiple consumers of a streamable string, the materialize function of the string module (http://zorba.io/modules/string) should be used to materialize the entire contents in an (regular) xs:string item.
import module namespace file = "http://expath.org/ns/file"; import module namespace string = "http://zorba.io/modules/string"; let $x := string:materialize(file:read-text("myfile.txt"))) return ($x, $x)In this example, the function returns a streamable string whose contents is used twice in the query. In order to be able to use the value twice, the string:materialize function must be used to materialize the entire contents of the file myfile.txt in memory. Otherwise, the error zerr:ZSTR0055 is raised.
declare function local:fib($n as xs:integer) as xs:integer { if ($n eq 0) then 0 else if ($n eq 1) then 1 else local:fib($n - 1) + local:fib($n - 2) }; local:fib(100)Specifically, this optimization reduces the complexity of the function from O(1.6^n) to O(n).In order to explicitly disable function caching, the user can specify the an:no-cache annotation.In addition, the user can use the an:cache annotation to cache the results of functions other than the ones that are automatically cached. However, this will only work if the function is not updating and its parameter and return types are subtypes of xs:anyAtomicType; otherwise, Zorba will raise a warning (zwarn:ZWST0005) and simply ignore the an:cache annotation.Please note, that explicitly enforcing caching for sequential or nondeterministic functions might not give the intended result. In such cases, Zorba will raise a warning (zwarn:ZWST0006) but obey the an:cache annotation.
import module namespace db = "http://zorba.io/modules/store/dynamic/collections/dml"; declare namespace ext = "http://zorba.io/extensions"; let $n := <a/> return (# ext:no-copy #) { $n is db:apply-insert-last(xs:QName("local:bar"), $n) }