saxon:expression

Creates a stored expression that can be evaluated repeatedly with different argument values and a different dynamic context.

expression($string as xs:string) ➔ jt:net.sf.saxon.functions.Evaluate-PreparedExpression

Arguments

 

$string

xs:string

The expression to be compiled

Result

jt:net.sf.saxon.functions.Evaluate-PreparedExpression

expression($string as xs:string, $ns as element()) ➔ jt:net.sf.saxon.functions.Evaluate-PreparedExpression

Arguments

 

$string

xs:string

The expression to be compiled

 

$ns

element()

Provides the namespace context

Result

jt:net.sf.saxon.functions.Evaluate-PreparedExpression

Namespace

http://saxon.sf.net/

Saxon availability

Requires Saxon-PE or Saxon-EE.

Notes on the Saxon implementation

Available since before Saxon 7.2. Dropped in 12.0, but then reinstated in 12.4 for the benefit of XQuery users, where the xsl:evaluate instruction is not available.

Details

This function creates a stored expression that can be evaluated repeatedly with different argument values and a different dynamic context.

The supplied string must contain an XPath expression. The result of the function is a stored expression, which may be supplied as an argument to other extension functions such as saxon:eval(). The result of the expression will usually depend on the context item. The context for the expression includes the namespaces in scope at this point in the stylesheet. The expression may contain references to the nine variables $p1, $p2, ... $p9 only. It may contain calls on Java extension functions, including Saxon-defined, EXSLT-defined, and EXPath-defined functions, as well as user-defined functions declared within the containing query or stylesheet. But it does not allow access to stylesheet variables, or functions defined in the XSLT specification such as key() or format-number().

If stylesheet functions are to be called from the expression, they should be declared with visibility="public".

Use of the current() function is allowed, provided saxon:evaluate() is called from XSLT: the value is the context item at the point where saxon:evaluate() or saxon:eval() is called. Use of other XSLT-specific functions such as key() or format-number() is untested; they might work in some cases and fail in others, possibly in an obscure way.

If the second argument is present, its value must be a single element node. The in-scope namespace bindings of this element node will be used to resolve any namespace prefixes present in the XPath expression. The default namespace for the element is also used as the default namespace for elements and types within the XPath expression. In addition, standard namespace bindings are automatically available for the prefixes xml, xs, xsi, fn, and saxon.

If the second argument is omitted, then the namespace context for the expression is taken from the stylesheet. (This is also the rule used by saxon:evaluate().) If the expression contains namespace prefixes, these are interpreted in terms of the namespace declarations in scope at the point where the saxon:expression() function is called, not those in scope where the stored expression is evaluated.

The stored expression (if it is to be evaluated using saxon:eval()) may contain references to variables named $p1, $p2, ... $p9. The values of these variables can be supplied when the expression is evaluated using saxon:eval(). The second argument of saxon:eval() supplies the value of $p1, the third argument supplies the value of $p2, and so on.

For example, following <xsl:variable name="add" select="saxon:expression('$p1 + $p2')"/>, the instruction <xsl:value-of select="saxon:eval($add, 6, 7)"/> will output 13.

See also:

saxon:eval()

saxon:evaluate()