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

Re: Filling table cells with <item>s

Subject: Re: Filling table cells with <item>s
From: "Joerg Heinicke" <joerg.heinicke@xxxxxx>
Date: Sat, 6 Apr 2002 02:17:08 +0200
xsl table empty cells
Hello Steve, you don't need to calculate the tables are something similar.
You only have to say "a new table for every 6th element", the same for a new
row. So the stylesheet is relative simple:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"

<xsl:param name="cols" select="2"/>
<xsl:param name="rows" select="3"/>
<xsl:param name="by" select="'col'"/>

<xsl:template match="root">
            <xsl:apply-templates select="item[position() mod ($rows * $cols)
= 1]" mode="table"/>

<xsl:template match="item" mode="table">
    <table border="1">
        <xsl:variable name="items-in-this-table" select=". |
following-sibling::item[position() &lt; ($rows * $cols)]"/>
            <xsl:when test="$by = 'row'">
                <xsl:apply-templates select="$items-in-this-table[position()
mod $cols = 1]" mode="row"/>
            <xsl:when test="$by = 'col'">
                <xsl:apply-templates select="$items-in-this-table[position()
&lt;= $rows]" mode="row"/>
        <!-- here you can implement some logic for filling the last table,
if there are not enough items to fill all <td>s -->

<xsl:template match="item" mode="row">
            <xsl:when test="$by = 'row'">
                <xsl:apply-templates select=". |
following-sibling::item[position() &lt; $cols]"/>
            <xsl:when test="$by = 'col'">
                <xsl:apply-templates select="(. |
following-sibling::item)[position() &lt; ($cols * $rows)][position() mod
$rows = 1]"/>

<xsl:template match="item">
        <xsl:value-of select="."/>


At first you access the item elements which should create a new table (every
6th item), so have to access every item element whose positions are 1
(counting 1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,...). This happens with "mod". The
second step is to access the current ( here: . ) item again and all its
following-sibling 2,3,4,5, of course with a new mode. This is done twice
(new table, new row) and everything is done.
When filling the table by col there must be used another logic. You can't
create the tables in HTML by cols. So we create a new <tr> for the first
$rows elements in this table. And then every $rows'th following element must
be added to this row.
1 bug: 1 row or 1 col isn't possible for one or the other. Any number mod 1
is always 0 ... but there is no element with position() 0. But you can fix
this too, if you need it. It's a bit more to write.

Hope this helps,


> If each table must have nrows and ncols, then a total of
>   ntables=ceiling(n/(nrows*ncols))
> tables are necessary to exhaust all of the <item>s with the
> last table possibly having some empty cells. For example
> if n=15, nrows=3 and ncols=2, ntable=3 and the three tables
> would be arranged one after the other as follows:
>    +------+------+
>    |item01|item02|
>    +------+------+
>    |item03|item04|
>    +------+------+
>    |item05|item06|
>    +------+------+
>    +------+------+
>    |item07|item08|
>    +------+------+
>    |item09|item10|
>    +------+------+
>    |item11|item12|
>    +------+------+
>    +------+------+
>    |item13|item14|
>    +------+------+
>    |item15|      |
>    +------+------+
>    |      |      |
>    +------+------+
> Also, I would like the option to fill the tables up by
> rows (as shown above) or by columns:
>    +------+------+
>    |item01|item04|
>    +------+------+
>    |item02|item05|
>    +------+------+
>    |item03|item06|
>    +------+------+
> How would you go about implementing this in XSLT given
> parameters nrows, ncols, by={"cols","rows"}? I am having
> some trouble determining what my "outer" loop is since there
> is the possibility of some empty cells (I can't just process
> each <item>). Thx in advance.

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

Current Thread


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.
First Name
Last Name
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.