[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: RE: two columns in a table from one

Subject: Re: RE: two columns in a table from one
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 30 Jul 2001 21:18:33 -0700 (PDT)
select two columns as one
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.