xsl-list
[Top] [All Lists]

Re: dynamic html table generation

2003-10-26 00:41:36
I have the following problem. I do have some elements (not knowing their
count) and want to insert them into table-columns where always after 4
inserted elements the next 4 elements should be stored in the next column.
I
have alreaday done this with always beginning a new _row_ every 4
elements.

[snip]

But how can that be done with columns?

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:template match="/">
    <html>
      <body>
        <table>
          <xsl:call-template name="makeNRowsTable">
            <xsl:with-param name="pcolSize" select="4"/>
            <xsl:with-param name="ptheNodes" select="/*/*"/>
          </xsl:call-template>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="makeNRowsTable">
    <xsl:param name="pcolSize" select="1"/>
    <xsl:param name="ptheNodes" select="/.."/>

    <xsl:for-each select="$ptheNodes[position() &lt;= $pcolSize]">
     <xsl:variable name="vthisMod" select="position() mod $pcolSize"/>
     <tr>
       <xsl:for-each
            select="$ptheNodes[position() mod $pcolSize = $vthisMod]">
          <td><xsl:value-of select="."/></td>
       </xsl:for-each>
     </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

when applied on this source.xml:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

produces the wanted result:

<html>
  <body>
    <table>
      <tr>
        <td>01</td>
        <td>05</td>
        <td>09</td>
      </tr>
      <tr>
        <td>02</td>
        <td>06</td>
        <td>10</td>
      </tr>
      <tr>
        <td>03</td>
        <td>07</td>
      </tr>
      <tr>
        <td>04</td>
        <td>08</td>
      </tr>
    </table>
  </body>
</html>

Note that the "makeNRowsTable" template accepts an arbitrary node-set, whose
elements may or may not be siblings. This is an improvement over the
"traditional" algorithm, which assumes that all nodes in the node-set are
siblings.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL








 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>