Nic Miloslav     Example 35    KEYWORDS      EXAMPLES      AUTHORS     

Parameters for a template can be passed with xsl:with-param element. If the template contains a xsl:param element with the same name as name attribute of xsl:with-param, this value is used. Stylesheet 1 shows a typical example. If you want to pass a variable, you have to define this variable with xsl:param element. Look at Stylesheet 2 for wrong approach.


     XML     HOME     XSL 1     XSL 2      
<xslTutorial >
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
</xslTutorial>

     XSL 1     HOME     XML     HTML 1     OUTPUT 1     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//number">
<TR><TH>
<xsl:choose>
<xsl:when test='text() mod 2'>
<xsl:apply-templates select=".">
<xsl:with-param name="type">odd</xsl:with-param>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</TH></TR>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template match="number">
<xsl:param name="type">even</xsl:param>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select='$type'/>
<xsl:text> )</xsl:text>
</xsl:template>
</xsl:stylesheet>

     HTML 1     HOME     XML     XSL 1     OUTPUT 1     
<HTML>
<HEAD> </HEAD>
<BODY>
<TABLE>
<TR>
<TH>1 (odd)</TH></TR>
<TR>
<TH>3 (odd)</TH></TR>
<TR>
<TH>4 (even)</TH></TR>
<TR>
<TH>17 (odd)</TH></TR>
<TR>
<TH>8 (even)</TH></TR></TABLE> </BODY> </HTML>

     OUTPUT 1     HOME     XML     XSL 1     HTML 1     
1 (odd)
3 (odd)
4 (even)
17 (odd)
8 (even)

     XSL 2     HOME     XML     HTML 2     OUTPUT 2     
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//number">
<TR><TH>
<xsl:choose>
<xsl:when test='text() mod 2'>
<xsl:apply-templates select=".">
<xsl:with-param name="type">odd</xsl:with-param>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</TH></TR>
</xsl:for-each>
</TABLE>
</xsl:template>
<xsl:template match="number">
<xsl:variable name="type">even</xsl:variable>
<xsl:value-of select="."/>
<xsl:text> (</xsl:text>
<xsl:value-of select='$type'/>
<xsl:text> )</xsl:text>
</xsl:template>
</xsl:stylesheet>

     HTML 2     HOME     XML     XSL 2     OUTPUT 2     
<HTML>
<HEAD> </HEAD>
<BODY>
<TABLE>
<TR>
<TH>1 (even)</TH></TR>
<TR>
<TH>3 (even)</TH></TR>
<TR>
<TH>4 (even)</TH></TR>
<TR>
<TH>17 (even)</TH></TR>
<TR>
<TH>8 (even)</TH></TR></TABLE> </BODY> </HTML>

     OUTPUT 2     HOME     XML     XSL 2     HTML 2     
1 (even)
3 (even)
4 (even)
17 (even)
8 (even)