Nic Miloslav     Example 81    KEYWORDS      EXAMPLES      AUTHORS     

Stylesheet 1 generates a table with selected elements, with the number of elements per row given in the stylesheet. If the elements should be sorted, the solution is more complex (Stylesheet 2).


     XML     HOME     XSL 1     XSL 2      
<xslTutorial >
<data>
<item>Fe</item>
<item>Cl</item>
<item>Br</item>
<item>I</item>
<item>Ni</item>
<item>H</item>
<item>Po</item>
<item>S</item>
<item>O</item>
</data>
</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 border='1'>
<xsl:variable name="inRow" select='3'/>
<xsl:apply-templates select="//item[position() mod $inRow = 1]">
<xsl:with-param name='inRow' select='$inRow'/>
</xsl:apply-templates>
</TABLE>
<TABLE border='1'>
<xsl:variable name="inRow" select='4'/>
<xsl:apply-templates select="//item[position() mod $inRow = 1]">
<xsl:with-param name='inRow' select='$inRow'/>
</xsl:apply-templates>
</TABLE>
<TABLE border='1'>
<xsl:variable name="inRow" select='5'/>
<xsl:apply-templates select="//item[position() mod $inRow = 1]">
<xsl:with-param name='inRow' select='$inRow'/>
</xsl:apply-templates>
</TABLE>
</xsl:template>
<xsl:template match="item">
<xsl:param name="inRow"/>
<TR>
<TD><xsl:value-of select="."/></TD>
<xsl:apply-templates select="following::item[position() &lt; $inRow]" mode='cell'/>
</TR>
</xsl:template>
<xsl:template match="item" mode='cell'>
<xsl:param name="inRow"/>
<TD><xsl:value-of select="."/></TD>
</xsl:template>
</xsl:stylesheet>

     HTML 1     HOME     XML     XSL 1     OUTPUT 1     
<TABLE border="1">
<TR>
<TD>Fe</TD>
<TD>Cl</TD>
<TD>Br</TD></TR>
<TR>
<TD>I</TD>
<TD>Ni</TD>
<TD>H</TD></TR>
<TR>
<TD>Po</TD>
<TD>S</TD>
<TD>O</TD></TR></TABLE>
<TABLE border="1">
<TR>
<TD>Fe</TD>
<TD>Cl</TD>
<TD>Br</TD>
<TD>I</TD></TR>
<TR>
<TD>Ni</TD>
<TD>H</TD>
<TD>Po</TD>
<TD>S</TD></TR>
<TR>
<TD>O</TD></TR></TABLE>
<TABLE border="1">
<TR>
<TD>Fe</TD>
<TD>Cl</TD>
<TD>Br</TD>
<TD>I</TD>
<TD>Ni</TD></TR>
<TR>
<TD>H</TD>
<TD>Po</TD>
<TD>S</TD>
<TD>O</TD></TR></TABLE>

     OUTPUT 1     HOME     XML     XSL 1     HTML 1     
FeClBr
INiH
PoSO
FeClBrI
NiHPoS
O
FeClBrINi
HPoSO

     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="/">
<xsl:call-template name="generateTable">
<xsl:with-param name="inRow" select="3"/>
</xsl:call-template>
<xsl:call-template name="generateTable">
<xsl:with-param name="inRow" select="4"/>
</xsl:call-template>
<xsl:call-template name="generateTable">
<xsl:with-param name="inRow" select="5"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generateTable">
<xsl:param name="inRow"/>
<xsl:variable name="text">
<xsl:for-each select="//item">
<xsl:sort order="ascending" select="."/>
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test='position()=last()'>
<xsl:text> XCELLSXXROWSX</xsl:text>
</xsl:when>
<xsl:when test='position() mod $inRow'>
<xsl:text> XCELLSX</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> XCELLSXXROWSX</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<TABLE border="1">
<xsl:call-template name="rows">
<xsl:with-param name="string" select="$text"/>
</xsl:call-template>
</TABLE>
</xsl:template>
<xsl:template name="rows">
<xsl:param name="string"/>
<TR>
<xsl:call-template name="cells">
<xsl:with-param name='string'><xsl:value-of select="substring-before($string,'XROWSX')"/>
</xsl:with-param>
</xsl:call-template>
</TR>
<xsl:if test="string-length($string)">
<xsl:call-template name="rows">
<xsl:with-param name='string'><xsl:value-of select="substring-after($string,'XROWSX')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="cells">
<xsl:param name="string"/>
<TD>
<xsl:value-of select="substring-before($string,'XCELLSX')"/>
</TD>
<xsl:if test="string-length($string)">
<xsl:call-template name="cells">
<xsl:with-param name='string'><xsl:value-of select="substring-after($string,'XCELLSX')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

     HTML 2     HOME     XML     XSL 2     OUTPUT 2     
<TABLE border="1">
<TR>
<TD>Br</TD>
<TD>Cl</TD>
<TD>Fe</TD>
<TD/></TR>
<TR>
<TD>H</TD>
<TD>I</TD>
<TD>Ni</TD>
<TD/></TR>
<TR>
<TD>O</TD>
<TD>Po</TD>
<TD>S</TD>
<TD/></TR>
<TR>
<TD/></TR></TABLE>
<TABLE border="1">
<TR>
<TD>Br</TD>
<TD>Cl</TD>
<TD>Fe</TD>
<TD>H</TD>
<TD/></TR>
<TR>
<TD>I</TD>
<TD>Ni</TD>
<TD>O</TD>
<TD>Po</TD>
<TD/></TR>
<TR>
<TD>S</TD>
<TD/></TR>
<TR>
<TD/></TR></TABLE>
<TABLE border="1">
<TR>
<TD>Br</TD>
<TD>Cl</TD>
<TD>Fe</TD>
<TD>H</TD>
<TD>I</TD>
<TD/></TR>
<TR>
<TD>Ni</TD>
<TD>O</TD>
<TD>Po</TD>
<TD>S</TD>
<TD/></TR>
<TR>
<TD/></TR></TABLE>

     OUTPUT 2     HOME     XML     XSL 2     HTML 2     
BrClFe
HINi
OPoS
BrClFeH
INiOPo
S
BrClFeHI
NiOPoS