Search code examples
exceptionxslt-1.0osb

How to raise an error inside an XLST1.0 template?


Since I could not find anything on the web (not even here on stack overflow), I hope you could help me find out how to raise an exception during the runtime of an XSL-Transformation. It runs inside the Oracle Service Bus 11, so we have only XLST1.0 features :(

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="//DATA">
        <parsed>
            <xsl:for-each select="ITEM">
                <xsl:choose>
                    <xsl:when test="COND = 'X'">
                        <xsl:text>disabled</xsl:text>
                    </xsl:when>
                    <xsl:when test="COND = ''">
                        <xsl:text>running</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>HERE SHOULD AN ERROR BE RAISED!</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </parsed>
    </xsl:template>

</xsl:stylesheet>

Solution

  • You can make use of the very handy xsl:message with the terminate="yes|no" attribute. In your case you may implement it as below:

    <xsl:template match="//DATA">
        <parsed>
            <xsl:for-each select="ITEM">
                <xsl:choose>
                    <xsl:when test="COND = 'X'">
                        <xsl:text>disabled</xsl:text>
                    </xsl:when>
                    <xsl:when test="COND = ''">
                        <xsl:text>running</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:message terminate="yes">HERE SHOULD AN ERROR BE RAISED!</xsl:message>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </parsed>
    </xsl:template>
    

    I recommend looking at the above-linked documentation, as some great ways to use xsl:message are delineated.