generates a table with selected elements,with the number of elements per row given in the stylesheet : table « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <element>Fe</element>
  <element>Cl</element>
  <element>Br</element>
  <element>I</element>
  <element>Ni</element>
  <element>H</element>
  <element>Po</element>
  <element>S</element>
  <element>O</element>
</data>
File: Transform.xslt

<?xml version="1.0" encoding="utf-8"?>
<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="//element[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="//element[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="//element[position() mod $inRow = 1]">
          <xsl:with-param name="inRow" select="$inRow"/>
        </xsl:apply-templates>
      </TABLE>
    </xsl:template>
    <xsl:template match="element">
      <xsl:param name="inRow"/>
      <TR>
        <TD>
          <xsl:value-of select="."/>
        </TD>
        <xsl:apply-templates select="following::element[position() &lt; $inRow]" mode="cell"/>
      </TR>
    </xsl:template>
    <xsl:template match="element" mode="cell">
      <xsl:param name="inRow"/>
      <TD>
        <xsl:value-of select="."/>
      </TD>
    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><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>








5.5.table
5.5.1.Use for-each loop to output table row
5.5.2.Table cell format
5.5.3.Format table cell with choose statement
5.5.4.Set table cell style with choose statement
5.5.5.generates a table with selected elements,with the number of elements per row given in the stylesheet