Nic Miloslav     Example 20    KEYWORDS      EXAMPLES      AUTHORS     

xsl:if instruction enables conditional processing. Stylesheet 1 demonstrates a typical case of xsl:for-each usage, adding a text between individual entries. Very often you do not want to add text after the last element. xsl-if construct comes handy here. (Stylesheet 2)


     XML     HOME     XSL 1     XSL 2      
<xslTutorial >
<list>
<entry name="A"/> 
<entry name="B"/> 
<entry name="C"/> 
<entry name="D"/> 
</list>
</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="list">
<xsl:for-each select="entry">
<xsl:value-of select="@name"/>
<xsl:text> , </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

     HTML 1     HOME     XML     XSL 1     OUTPUT 1     
<HTML>
<HEAD> </HEAD>
<BODY> A, B, C, D, </BODY> </HTML>

     OUTPUT 1     HOME     XML     XSL 1     HTML 1     
A, B, C, D,

     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="list">
<xsl:for-each select="entry">
<xsl:value-of select="@name"/>
<xsl:if test="not(position()=last())">
<xsl:text> , </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

     HTML 2     HOME     XML     XSL 2     OUTPUT 2     
<HTML>
<HEAD> </HEAD>
<BODY> A, B, C, D </BODY> </HTML>

     OUTPUT 2     HOME     XML     XSL 2     HTML 2     
A, B, C, D