Re: [xsl] Building tables with XSLT

Subject: Re: [xsl] Building tables with XSLT
From: juggy@xxxxxxx
Date: Tue, 10 Sep 2002 16:10:44 +0200
uhm, ups, I forgot the part about the not multiples-of four:
<xsl:template name="pos">
<xsl:param name="pos" />
<xsl:param name="width" />
<xsl:choose>
<xsl:when test="(position()+$width) < last()">
<xsl:for-each select="IMAGES/IMAGE[position() >= $pos and position() <($pos+$width)">
<td>...put your image...</td>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="left"><xsl:value-of select="$width- (last() - (position()))" /></xsl:variable>
<xsl:for-each select="IMAGES/IMAGE[position() >= $pos and position() <($pos+$width)">
<td>...put your image...</td>
</xsl:for-each>
<td colspan="$left"></td>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Just a crude hack - probably someone can come up with something better.

Greetings,

Juggy
Dan Henke wrote:
Hi,

I've included some sample XML and a cutdown XSLT stylesheet that I'm using
to generate a table 4 columns wide and as many rows long as needed with an
image in each cell.  The stylesheet also pads out the last few cells if
there isnt a multiple of 4 images.  The part I'm stuck on is taking this
working example and making it able to build a table any number of columns
wide dictated by a parameter passed to the template.  Another point I'm
stuck on is that the real life XML file might have 100 images in it and I am
hoping to be able to generate the table from a subset of those images.  For
example, given a long list of <IMAGE> nodes as below I might want to create
a table which is 5 columns wide and contains all of the images from position
20 to position 35.  I'm pushing my XSLT ability already so any help in
working this problem out would be greatly appreciated.

-Dan


Here is a fragment from my XML source:


<IMGAGES>
    <IMAGE HEIGHT="100" WIDTH="120">image01.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image02.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image03.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image04.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image05.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image06.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image07.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image08.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image09.gif</IMAGE>
    <IMAGE HEIGHT="100" WIDTH="120">image10.gif</IMAGE>
</IMAGES>

And a cutdown version of the XSLT file I'm using is like this:

<xsl:template match="/">
    <xsl:call-template name="displayRow">
        <xsl:with-param name="pos" select="1"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="displayRow">
    <xsl:param name="pos"/>
        <tr>
            <xsl:call-template name="displayCell">
                <xsl:with-param name="pos" select="$pos"/>
            </xsl:call-template>
            <xsl:call-template name="displayCell">
                <xsl:with-param name="pos" select="$pos + 1"/>
            </xsl:call-template>
            <xsl:call-template name="displayCell">
                <xsl:with-param name="pos" select="$pos + 2"/>
            </xsl:call-template>
            <xsl:call-template name="displayCell">
                <xsl:with-param name="pos" select="$pos + 3"/>
            </xsl:call-template>
        </tr>
        <xsl:if test="IMAGES/IMAGE[$pos + 4]">
            <xsl:call-template name="displayRow">
                <xsl:with-param name="pos" select="$pos + 4"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

<xsl:template name="displayCell">
    <xsl:param name="pos"/>
    <xsl:variable name="CELL" select="IMAGES/IMAGE[$pos]"/>
    <xsl:choose>
        <xsl:when test="$CELL">
            <td align="center" valign="middle">
                <img src="{$CELL}" height="{$CELL/@HEIGHT}"
width="{$CELL/@WIDTH}" border="0"/>
            </td>
        </xsl:when>
        <xsl:otherwise>
            <td>&#160;</td>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>



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




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


Current Thread