This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Building tables with XSLT


Hi Dan,

Seems like you are thinking about something like a varaiable update as used in ordinary programming languages. A solution that comes to my mind would be to do it as follows (I haven't tried it for bugs, but maybe this might help anyway):
<xsl:template>
<xsl:param name="width">4</xsl:param> <!-- standard width -->
<xsl:for-each select="IMAGES/IMAGE[(postion() % $width) = 0">
<tr><td>
<xsl:call-template name="rowdisplay">
<xsl:with-param name="pos"><xsl:value-of select="position" /></xsl:with-param>
<xsl:with.param name="width"><xsl:value-of select="$width" /></xsl:template>
</xsl:call-template>
</td></tr>
</xsl:for-each>
</xsl:template>
<xsl:template name="pos">
<xsl:param name="pos" />
<xsl:param name="width" />
<xsl:for-each select="IMAGES/IMAGE[position() >= $pos and position() <($pos+$width)">
<td>...put your image...</td>
</xsl:for-each>
</xsl:template>

Maybe this helps. But if anyone knew a way to change the value of a variable without nesting templates or such, I would like to hear about it - would prevent constructs like the above IMHO.

Regards,

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]