xsl:try
The xsl:try
instruction is new in XSLT 3.0: in conjunction with xsl:catch
it allows
recovery from dynamic errors.
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>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>The errors
attribute of xsl:catch
indicates which error codes are caught. If absent, or it set to
*
, all errors are caught. The value can be a whitespace-separated list of QNames; the wildcards *:local
or prefix:*
can also be used.
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.