Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Table with multiple columns (HTML)

0 views
Skip to first unread message

Fredrik Nordbakke

unread,
Apr 25, 2003, 8:17:33 AM4/25/03
to
XML file:

<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

Dimitre Novatchev

unread,
Apr 25, 2003, 9:17:26 AM4/25/03
to
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.

=====
Cheers,

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


"Fredrik Nordbakke" <fre...@fnprg.com> wrote in message
news:ro9iav8jsp51e3sda...@4ax.com...

Fredrik Nordbakke

unread,
Apr 26, 2003, 6:41:17 PM4/26/03
to
"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):

Dimitre Novatchev

unread,
Apr 27, 2003, 2:51:39 AM4/27/03
to

"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:

<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() &lt;= $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

unread,
Apr 27, 2003, 6:58:01 AM4/27/03
to
"Dimitre Novatchev" <dnova...@yahoo.com> wrote:

>
>"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!

karl...@hotmail.com

unread,
May 27, 2003, 12:32:40 PM5/27/03
to
I altered the code to:
a) work using attributes.
b) to close out table rows so there are no missing cells.
c) I used a more standard way of adding a background color (using the
xsl:attribute tag and CSS stylesheets)

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() &gt;=
$vCurPosition and position() &lt; $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() &lt; $numCols">
<td colspan="{number($numCols) - position()}">&#160;</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>

0 new messages