<xslTutorial creator="nicmila@idoox.com">
<index keywords='xsl:variable  last() position()'/>

<description>A stylesheet can contain several variables of the same name. <stylesheet id='id2'/> demonstrates a way how to recover the value of global variable which has the same name as a local one. The <stylesheet id='id3'/> demonstrates an incorrect approach. The value of local variable is bounded to xsl:when element. The rest of template therefore sees only the global variable. </description>

<xmlSource id="id1">
<chapter>Chapter A</chapter>
<chapter>Chapter B</chapter>
<chapter>Chapter C</chapter>
<chapter>Chapter D</chapter>
</xmlSource>

<attValues>
<value match=''></value>
</attValues>

<xslStylesheet id="id2">
<xsl:variable name="text">Chapter</xsl:variable>

<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR><TD>
<xsl:variable name="text">
<xsl:choose>
<xsl:when test='position() = 1'>First chapter</xsl:when>
<xsl:when test='position()=last()'>Last chapter</xsl:when>
<xsl:otherwise><xsl:value-of select="$text"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$text"/>
<xsl:text> : </xsl:text>
<xsl:value-of select="."/>
</TD></TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xslStylesheet>

<xslStylesheet id="id3">
<xsl:variable name="text">Chapter</xsl:variable>

<xsl:template match="/">
<TABLE>
<xsl:for-each select="//chapter">
<TR><TD>

<xsl:choose>
<xsl:when test='position() = 1'>
<xsl:variable name="text">First chapter</xsl:variable> 
</xsl:when>
<xsl:when test='position()=last()'>
<xsl:variable name="text">Last chapter</xsl:variable> 
</xsl:when>
<xsl:otherwise>
<xsl:variable name="text"><xsl:value-of select="$text"/></xsl:variable>
</xsl:otherwise>
</xsl:choose>

<xsl:value-of select="$text"/>
<xsl:text> : </xsl:text>
<xsl:value-of select="."/>
</TD></TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xslStylesheet>


</xslTutorial>