[xsl] Re: creating mutiple columns

Subject: [xsl] Re: creating mutiple columns
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 22 Jun 2001 02:58:13 -0700 (PDT)
Michael Zehrer wrote:
> 
> I have XML data in the format:
> 
> <node>
> <element>...<element/>
> <element>...<element/>
> <element>...<element/>
> ...
> </node>
> 
> In the HTML output I need to put these <element/> data in multiple columns
> with a maximum of 3 rows per column like this:
> 
> element1 | element4 | element7
> element2 | element5 | ...
> element3 | element6 | ...
> 
> Is there any way to do this in xsl?

Yes, but your sample xml input is not well-formed -- the syntax for end tags is
</someName>.

Given the following input:

<node>
    <element>element1</element>
    <element>element2</element>
    <element>element3</element>
    <element>element4</element>
    <element>element5</element>
    <element>element6</element>
    <element>element7</element>
    <element>element8</element>
    <element>element9</element>
    <element>element10</element>
    <element>element11</element>
</node>

the following stylesheet will transform it to a table having $elementsPerRow
elements per row (columns):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output indent="yes" omit-xml-declaration="yes"/>

  <xsl:param name="elementsPerRow" select="3"/>
  <xsl:template match="/">
     <table>
        <xsl:apply-templates
            select="/node/element[position() mod $elementsPerRow = 1]"
            mode="wrap"
        />
     </table>
  </xsl:template>
  <xsl:template match="element" mode="wrap">
      <tr>
        <td><xsl:value-of select="."/></td>
        <xsl:apply-templates select="following-sibling::element[position() &lt;
$elementsPerRow]"/>
      </tr>
  </xsl:template>
  <xsl:template match="element">
        <td><xsl:value-of select="."/></td>
  </xsl:template>
</xsl:stylesheet>


In your case $elementsPerRow is set to 3 and the output is the following:

<table>
<tr>
<td>element1</td>
<td>element2</td>
<td>element3</td>
</tr>
<tr>
<td>element4</td>
<td>element5</td>
<td>element6</td>
</tr>
<tr>
<td>element7</td>
<td>element8</td>
<td>element9</td>
</tr>
<tr>
<td>element10</td>
<td>element11</td>
</tr>
</table>

Cheers,
Dimitre Novatchev.






__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

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


Current Thread