Variables into XSLT

Generally speaking of XSLT as programming, it is natural to ask whether it is possible to supply parameters to an XSL Transformation. It is after all possible in probably all other languages.

Indeed in a minute you will, in the assignment, be required to select Subject 1 with xsl from the xml file, but then the user may click otherwise, and all of a sudden it is Subject 2, in other words you need to infuse the value of a variable into an xslt variable.

It is a simple two step procedure. First you must define a variable, parameter, in your xslt, then, secondly you must give it a value at runtime.

Example 43.11. Variable in XSLT

In your xsl file, just after the start tag of the root element, you might place:

<xsl:param name="variable1">Databases and XML</xsl:param>

And usage:

<xsl:template match="subject">
    <xsl:choose>
        <xsl:when test=". = $variable1"
            ...
        </xsl:when>
    </xsl:choose>
</xsl:template>

Example 43.12. JavaScript Aspect of Variable Value to XSLT
    xsltProc = new XSLTProcessor();
    xsltProc.importStylesheet(xsl);
    xsltProc.setParameter(null, "variable1", "IDE and CMS");
    // ...