XSLT 1.0 include

A few years back, I ran a technical session for a client, on how to use XSLT to solve tricky Siebel problems and i came to realise that not every developer is exposed to XSLT.

On some projects XSLT is handled by the middleware team, and Siebel professionals just send across the raw Siebel Message for the external team to perform the transformations.

In a recent discussion with a new client, they wanted to upgrade to XSLT 2.0 to take advantage of include in XSLT to reduce the size of all their XSLTs. I advised them that it was not necessary to upgrade the XSLT engine for this reason, because this feature already exists in XSLT 1.0.

Keen readers of Impossible Siebel would know that the engine behind the EAI XSLT Service, which is used to transform all XML in Siebel, uses XSLT 1.0.

Casual implementors of XSLT might not know about the include statement, but this feature allows you to declare all your XSLT functions in a seperate file, and import it into the current stylesheet.

The following sample shows how to include an external file containing some XLST functions.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="XSLTfunctions.xslt"/>
</xsl:stylesheet>

<!--XSLTfunctions.xsl-->
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" /> 
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 
    <xsl:template name="FORMATBOOLEAN">
        <xsl:param name="BooleanChar"/>
        <xsl:choose>             
            <xsl:when test="translate($BooleanChar, $smallcase, $uppercase)='Y'">true</xsl:when>
            <xsl:when test="translate($BooleanChar, $smallcase, $uppercase)='YES'">true</xsl:when>
            <xsl:when test="translate($BooleanChar, $smallcase, $uppercase)='TRUE'">true</xsl:when>
            <xsl:otherwise>false</xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

In the above example, i've presented a function to ensure that booleans are formatted consistently, this can helpful if you have different source systems that have slightly different representations of booleans.

Since XSLT 1.0 dosnt have upper-case() and lower-case() functions, i used the translate function to perform this operation. The use of translate can be further refactored into a seperate function for speed.

Finally, here is how you would call these external included functions from your XSLT.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:include href="XSLTfunctions.xslt"/>
    
    <xsl:template match="/">
    <xsl:call-template name="FORMATBOOLEAN">
        <xsl:with-param name="BooleanChar">
            <xsl:text>y</xsl:text>
        </xsl:with-param>
    </xsl:call-template>
    </xsl:template >
</xsl:stylesheet>


Result: true

Using the "include" allows you to easily maintain all your XSLT functions in one place, it will reduce the size and clutter of your stylesheets, as well make it lighter and hence perform better, and maximise its re-usability.

In the next article, we'll look at how we can optimize the above function to perform better.


Reactions:

0 comments:

Post a Comment

Comments are open to all, please make it constructive.