# saxon:expression()

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

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

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 current node. 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 and EXSLT-defined functions, as well as user-defined function 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()`.

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 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`.

Next