Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing an exception from XSLT

Tags:

xml

xslt

railo

I need XSLT to stop processing with an error, when an external document is not available. From what I've found out the <xsl:message> tag seems to be the right way to do it, but so far it doesn't work. Here's what I tried:

<xsl:if test="not(document('some_external_doc.xml')//myxpath)">
    <xsl:message terminate="yes">ERROR: Missing element!</xsl:message>
    <h1>Error detected!</h1>
</xsl:if>

The missing document/xpath is detected by the <xsl:if> and the <h1> will be displayed, but for some reason the terminate attribute of the <xsl:message> is ignored. The transformation is done in Railo, so the XSLT processor use should be the Java default, but I wasn't able to find something definitive about the processor Railo is using.

like image 510
korguell Avatar asked Nov 21 '25 22:11

korguell


1 Answers

You have the right idea, however...

If your XSLT processor implements XSLT 1.0, it technically does not have to terminate. Note the use of the word should rather than must in the spec for xsl:message:

If the terminate attribute has the value yes, then the XSLT processor should terminate processing after sending the message. The default value is no.

Interestingly, XSLT 2.0 changes the should to a must:

If the effective value of the terminate attribute is yes, then the processor must terminate processing after sending the message.

Note also that order of execution of xsl:message statements is processor-dependent; keep this in mind when looking for xsl:message output in the logs.

Finally, you have some additional exception processing options under XSLT 2.0 (error() function) and XSLT 3.0 (xsl:try and xsl:catch).

like image 130
kjhughes Avatar answered Nov 23 '25 14:11

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!