>> English << | Français | Deutsch | Magyar | 中文 | Polski ZVON > Tutorials > XSLT Tutorial
>> Page 34 << | Prev | Next | Contents | Element Index

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. XSLT 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 XSLT stylesheet 2 for wrong approach.

XSLT stylesheet 1

XML Source
<source>

<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>

</source>

Output
<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>

HTML view
1 (odd)
3 (odd)
4 (even)
17 (odd)
8 (even)
XSLT stylesheet
<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>


XSLT stylesheet 2

XML Source
<source>

<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>

</source>

Output
<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>

HTML view
1 (even)
3 (even)
4 (even)
17 (even)
8 (even)
XSLT stylesheet
<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>