<items>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</items>
I would like to create a table like this:
1 5
2 6
3 7
4 8
The number of items is not known in advance.
How can this be achieved?
---
Fredrik Nordbakke
FNProgramvare - http://www.fnprg.com
This snippet shows how to produce a table of N columns and have every second
row in another colour.
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Fredrik Nordbakke" <fre...@fnprg.com> wrote in message
news:ro9iav8jsp51e3sda...@4ax.com...
>http://www.topxml.com/code/default.asp?p=3&id=v20020514091249
>
>This snippet shows how to produce a table of N columns and have every second
>row in another colour.
Thanks for the answer, this this template produces a table like
this (left to right):
1 2
3 4
5 6
7 8
I want to create a table like this (top to bottom):
This is quite similar.
This transformation:
<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[position() <= $numCols]/name">
<xsl:with-param name="numCols" select="number($numCols)" />
<xsl:with-param name="nodes" select="/phonelist/person/name" />
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="*" mode="multiColumn">
<xsl:param name="numCols" select="4"/>
<xsl:param name="nodes" select="/.."/>
<xsl:variable name="vcurPosition" select="position()"/>
<tr>
<xsl:apply-templates mode="normal"
select="$nodes[position() mod $numCols
=
$vcurPosition mod $numCols]"/>
</tr>
</xsl:template>
<xsl:template match="*" mode="normal">
<td>
<xsl:value-of select="." />
</td>
</xsl:template>
</xsl:stylesheet>
when applied on this 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>
Produces the desired 4-column table:
<table>
<tr>
<td>Adrian</td>
<td>Henry</td>
<td>John</td>
<td>Nick</td>
</tr>
<tr>
<td>Alex</td>
<td>Ian</td>
<td>Kurt</td>
<td>Olivia</td>
</tr>
<tr>
<td>Andrew</td>
<td>Ivan</td>
<td>Lesley</td>
</tr>
<tr>
<td>Hayley</td>
<td>Jeanne</td>
<td>Martin</td>
</tr>
</table>
Hope this helped.
>
>"Fredrik Nordbakke" <fre...@fnprg.com> wrote in message
>news:kj2mavscb7o7i6jpc...@4ax.com...
>> "Dimitre Novatchev" <dnova...@yahoo.com> wrote:
>>
>> >http://www.topxml.com/code/default.asp?p=3&id=v20020514091249
>> >
>> >This snippet shows how to produce a table of N columns and have every
>second
>> >row in another colour.
>>
>> Thanks for the answer, this this template produces a table like
>> this (left to right):
>>
>> 1 2
>> 3 4
>> 5 6
>> 7 8
>>
>> I want to create a table like this (top to bottom):
>>
>> 1 5
>> 2 6
>> 3 7
>> 4 8
>
>This is quite similar.
>
>This transformation:
Thanks!
Thanks for posting this ... it saved me a lot of headaches :)
Cheers,
DK
<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="3" />
<xsl:variable name="fullCount" select="count(/phonelist/people/name)"
/>
<xsl:template match="/">
<style>
table tr.item0 td {background:#f0f0f0;}
table tr.item1 td {background:#cccccc;}
</style>
<table border="1">
<xsl:apply-templates select="/phonelist/people/name"
mode="multiColumn">
<xsl:with-param name="numCols" select="$numCols" />
<xsl:with-param name="nodes" select="/phonelist/people/name" />
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="*" mode="multiColumn">
<xsl:param name="nodes" select="." />
<xsl:variable name="vCurPosition" select="position()" />
<xsl:if test="$vCurPosition mod $numCols = 1">
<!-- alternate the background coloring for odd/even row -->
<tr>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="$vCurPosition mod (2 * $numCols) =
1">item0</xsl:when>
<xsl:otherwise>item1</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates mode="normal" select="$nodes[position() >=
$vCurPosition and position() < $vCurPosition + $numCols]" />
</tr>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="normal">
<td>
<xsl:value-of select="@val" />
<!-- Close out row with extra spaces -->
<xsl:if test="position()=last() and position() < $numCols">
<td colspan="{number($numCols) - position()}"> </td>
</xsl:if>
</td>
</xsl:template>
</xsl:stylesheet>
---------------------------------------
<phonelist datecreated="10/01/2001">
<people>
<name val="Adrian"/>
<name val="Andrew"/>
<name val="Alex"/>
<name val="Hayley"/>
<name val="Henry"/>
<name val="Ian"/>
<name val="Ivan"/>
<name val="Jeanne"/>
<name val="John"/>
<name val="Kurt"/>
<name val="Lesley"/>
<name val="Martin"/>
<name val="Nick"/>
<name val="Olivia"/>
<name val="Nicky"/>
<name val="Oliver"/>
</people>
</phonelist>