http://zorba.io/modules/reflection

View as XML or JSON.

This module provides functions to dynamically invoke functions or main modules, respectively. Each of the functions (invoke or eval) come in four variants depending whether the expression being invoked is simple, nondeterministic, updating, or sequential.

Function Summary

eval-n ($query as xs:string) as item()* external

See documentation of reflection:eval() except the main module that is to be executed may be nondeterministc.

eval-s ($query as xs:string) as item()* external

See documentation of reflection:eval() except the main module that is to be executed may be sequential, i.

eval-u ($query as xs:string) external

See documentation of reflection:eval() except the main module that is to be executed may be updating, i.

eval ($query as xs:string) as item()* external

The purpose of this function is to (dynamically) execute an XQuery program from inside another XQuery program.

invoke-n ($name as xs:QName) as item()* external

See documentation for reflection:invoke except the function that is to be invoked may be nondeterministic.

invoke-s ($name as xs:QName) as item()* external

See documentation for reflection:invoke except the function that is to be invoked may be sequential, i.

invoke-u ($name as xs:QName) external

See documentation for reflection:invoke-n except the function that is to be invoked may be updating, i.

invoke ($name as xs:QName) as item()* external

The invoke function allows to dynamically call a function given its QName and parameters.

Functions

eval-n#1

declare  %an:nondeterministic function reflection:eval-n($query as xs:string) as item()* external
See documentation of reflection:eval() except the main module that is to be executed may be nondeterministc.

Parameters

query as xs:string
the query string to be evaluated

Returns

item()*
the result of evaluating the query

eval-s#1

declare  %an:sequential function reflection:eval-s($query as xs:string) as item()* external
See documentation of reflection:eval() except the main module that is to be executed may be sequential, i.e. may have side-effects.

Parameters

query as xs:string
the query string to be evaluated

Returns

item()*
the result of evaluating the query (the result is not supposed to contain any PUL).

eval-u#1

declare  %an:nondeterministic function reflection:eval-u($query as xs:string) external
See documentation of reflection:eval() except the main module that is to be executed may be updating, i.e. return a pending update list.

Parameters

query as xs:string
the query string to be evaluated

Returns

the PUL resulting from evaluating the query

eval#1

declare  function reflection:eval($query as xs:string) as item()* external
The purpose of this function is to (dynamically) execute an XQuery program from inside another XQuery program.

The XQuery program that invokes the eval function will be referred to as the "outer" program and the XQuery program that is executed by the eval invocation will be referred to as the "inner" program. The function is given as a string argument. Typically, the outer program constructs this string dynamically, e.g., based on data extracted from documents and/or the values of external variables. The eval function treats this string as an XQuery main module. That is, it parses the string, compiles the resulting parse tree, executes the resulting execution plan, and finally returns the result or error (if any) to the outer program.

The given XQuery program needs to be a valid according to XQuery's MainModule production (see http://www.w3.org/TR/xquery/#doc-xquery-MainModule. Please note that the inner pogram must at least have the XQuery version of the outer program [err:XQST0031].

The inner program "inherits" the static and dynamic context of the outer program. Specifically, evaluation of the inner program is done in static and dynamic contextes that are initialized as copies of the static and dynamic contextes of the outer program at the place where the eval invocation appears at. This means that, for example, all variables that are in-scope at the place where the eval function is invoked from, are also in-scope inside the inner program and can be referenced there without having to be re-declared. On the other hand, declarations that appear in the prolog of the inner main module or are imported by the inner main module from library modules, hide their corresponding inherited declarations. For example, if the inner main module declares a variable or function with the same name as an inherited variable or function, the inner variable/function hides the inherited one.

If the inner program declares an external variable with the same name as an inherited variable, the value of the inherited variable is used to initialize the inner external variable. If, however, an inner external variable has no default initializer and no corresponding inherited variable, it will remain uninitialized, causing the inner program to raise an error when executed.

Parameters

query as xs:string
the query string to be evaluated

Returns

item()*
the result of evaluating the query

invoke-n#1

declare  %an:nondeterministic %an:variadic function reflection:invoke-n($name as xs:QName) as item()* external
See documentation for reflection:invoke except the function that is to be invoked may be nondeterministic.

Parameters

name as xs:QName
the QName of the function that is to be invoked

Returns

item()*
the result that is returned by the invoked function

invoke-s#1

declare  %an:variadic %an:sequential function reflection:invoke-s($name as xs:QName) as item()* external
See documentation for reflection:invoke except the function that is to be invoked may be sequential, i.e. may have side-effects.

Parameters

name as xs:QName
the QName of the function that is to be invoked

Returns

item()*
the result that is returned by the invoked function

invoke-u#1

declare  %an:nondeterministic %an:variadic function reflection:invoke-u($name as xs:QName) external
See documentation for reflection:invoke-n except the function that is to be invoked may be updating, i.e. return a pending update list.

Parameters

name as xs:QName
the QName of the function that is to be invoked

Returns

the result that is returned by the invoked function

invoke#1

declare  %an:variadic function reflection:invoke($name as xs:QName) as item()* external
The invoke function allows to dynamically call a function given its QName and parameters.

It is possible to invoke a function whose name is not known at compilation time -- it can be computed, passed through an external variable, taken from a file, etc. The first parameter must always be a QName identifying a known function.

The function is declared with the %an:variadic annotation. Hence, it allows for an arbitrary number of parameters. All of these parameters (except the first one) will be passed to the function that is called.
Example usage :

 reflection:invoke ( xs:QName("fn:max"), (1,2,3) ) 

Returns
 3 
.

Parameters

name as xs:QName
the QName of the function that is to be invoked

Returns

item()*
the result that is returned by the invoked function