[xsl] Re: RE: two columns in a table from one

Subject: [xsl] Re: RE: two columns in a table from one
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 30 Jul 2001 21:18:33 -0700 (PDT)
Tim Watts wrote:

> Hi Matt,
> 
> I had a simular problem which I asked on this list quite a while ago now.
> 
> (Thanks to Jeni who gave me a template for creating muli-column tables).
> 
> The example below is the one I used for a three column table...
> 
> <xsl:template name="threecolumns">
> <!-- this sorts the currencies into three columns -->
>   <xsl:for-each select="people[position() mod 3 = 1]">
>     <tr>
>     <xsl:variable name="others" select="following-sibling::option[position()
> &lt; 3]" />

[the rest snipped]

Hi Tim,

The above template works well when the nodes to be represented as 3-column table are
siblings.

Quite frequently this is not the case -- one needs to make a multicolumn table out
of arbitrary nodes contained in a node-set.

Bellow is a template (actually two of them as we need one for "normal processng")
that displays the contents of arbitrary ellements named "name" from a node-set into
a multicolumn table havng $numCols columns.

It also colours every odd row green and every even row red.

Do enjoy:


    <xsl:template match="name" mode="multiColumn">
      <xsl:param name="nodes" select="/.."/>
      <xsl:param name="numCols" select="1"/>
      
       <xsl:variable name="vCurPosition" select="position()"/>
       <xsl:choose>
        <xsl:when test="$vCurPosition mod (2 * $numCols) = 1">
          <tr bgcolor="green">
            <xsl:apply-templates mode="normal"
                      select="$nodes
                                 [position() >= $vCurPosition
                                 and
                                  position() &lt; $vCurPosition + $numCols]"
            />
          </tr>
        </xsl:when>
        <xsl:when test="$vCurPosition mod $numCols = 1">
          <tr bgcolor="red">
            <xsl:apply-templates mode="normal"
                      select="$nodes
                                 [position() >= $vCurPosition
                                 and
                                  position() &lt; $vCurPosition + $numCols]"
            />
          </tr>
        </xsl:when>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template match="name" mode="normal">
     <td><xsl:value-of select="."/></td>
    </xsl:template>


If we have the following source xml:

<phonelist datecreated="10/01/2001">
	<person>
		<name>Adrian</name>
	</person>
	<person>
		<name>Alex</name>
	</person>
	<person>
		<name>Andrew</name>
	</person>	
	<person>
		<name>Hayley</name>
	</person>
	<person>
		<name>Henry</name>
	</person>
	<person>
		<name>Ian</name>
	</person>
	<person>
		<name>Ivan</name>
	</person>
	<person>
		<name>Jeanne</name>
	</person>
	<person>
		<name>John</name>
	</person>
	<person>
		<name>Kurt</name>
	</person>
	<person>
		<name>Lesley</name>
	</person>
	<person>
		<name>Martin</name>
	</person>
	<person>
		<name>Nick</name>
	</person>
	<person>
		<name>Olivia</name>
	</person>
</phonelist>

and a stylesheet that has:

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

    <xsl:template match="/">
      <table>
        <xsl:apply-templates mode="multiColumn"
                             select="/phonelist/person/name">
          <xsl:with-param name="numCols" select="$numCols"/>
          <xsl:with-param name="nodes"
                        select="/phonelist/person/name"
          />
        </xsl:apply-templates>
      </table>
    </xsl:template>
    

Then the result is the followng 3-column alternating-coloured row table:

Adrian Alex Andrew Hayley 
Henry Ian Ivan Jeanne 
John Kurt Lesley Martin 
Nick Olivia 

 
Hope this helped.

Cheers,
Dimtre 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