Throwing an exception from XSLT

2019-07-16 01:26发布

问题:

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.

回答1:

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



标签: xml xslt railo