xsl:try
In conjunction with xsl:catch, the
xsl:try
instruction allows recovery from dynamic errors
occurring within the expression it wraps.
Category: instruction
Content: (
sequence-constructor
, xsl:catch
, (
xsl:catch
| xsl:fallback
)*
)
Permitted parent elements:
any XSLT element whose content model is
sequence-constructor; any literal result element
Attributes
|
|
The expression to be evaluated by the
instruction may be defined either by a |
|
|
The value |
Notes on the Saxon implementation
Note that within an xsl:try
instruction,
disable-output-escaping
has no effect. (According to the
specification, "when writing to a final result tree that is not being
serialized, the request to disable output escaping is ignored.")
Saxon (9.8) currently ignores the value of the rollback-output attribute.
Details
It is possible to have more than one xsl:catch
within an
xsl:try
; the first one that matches the error is used.
Within the xsl:catch
, a number of variables are available in the
namespace http://www.w3.org/2005/xqt-errors
:
-
err:code
- the error code as a QName -
err:description
- the error description (error message) -
err:value
- the error object (if available) -
err:module
- the URI of the stylesheet module in which the error occurred -
err:line-number
- the line-number of the source stylesheet where the error occurred -
err:column-number
- for Saxon this will generally be unknown (-1)
The error can be re-thrown by using the error() function.
Examples
Example 1
The following example shows how to recover from an error in evaluating an XPath expression (in this case, divide-by-zero):
<xsl:try select="salary div length-of-service"> <xsl:catch errors="err:FAOR0001" select="()"/> </xsl:try>Example 2
The following example shows how to recover from an error in evaluating a sequence of XSLT instructions (in this case, a validation error):
<xsl:try> <xsl:copy-of select="$result" validation="strict"/> <xsl:catch> <xsl:message>Warning: validation of result document failed: Error code: <xsl:value-of select="$err:code"/> Reason: <xsl:value-of select="$err:description"/> </xsl:message> <xsl:sequence select="$result"/> </xsl:catch> </xsl:try>