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,

well, its quite easy, all you need to do is get familiar with recursive
templates.

here's the stylesheet, I have tested it and it worked ok. I commented some
parts of the code, this should reduce the overall size.

<?xml version="1.0"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <!-- defaults to first images in database : 1 -->
    <xsl:param name="start" select="'1'"/>
    <!-- defaults to 5 columns / row: 5 -->
    <xsl:param name="columns" select="'5'"/>
    <!-- defaults to 15 images / page, that is, 3 rows / page -->
    <xsl:param name="count" select="'15'"/>

    <!-- These parameters then would need some adjustments, namely, declare
a global variable which holds
            the number of rows to be displayed

            $count div $columns = $rows
    -->
    <xsl:variable name="rows" select="$count div $columns"/>

<xsl:template match="/">
    <xsl:call-template name="displayRows">
        <xsl:with-param name="dr_start" select="$start"/>
        <xsl:with-param name="dr_columns" select="$columns"/>
        <xsl:with-param name="dr_rows" select="$rows"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="displayRows">
    <xsl:param name="dr_start"/>
    <xsl:param name="dr_columns"/>
    <xsl:param name="dr_rows"/>

    <!-- we are counting from n to 0 downwards here -->
    <xsl:if test="$dr_rows > 0">
        <tr>
            <!-- display the columns -->
                <!-- adjust start, so that it correctly adjusts to all the
                       rows and columns being displayed previously

                        that is: the new start position (the image number)
                        is equal to:

                            $pr_rows = ($rows - $dr_rows) : number of
already processed rows

                            $start = ($pr_rows * $columns) + $start

                            the new start position for the next row and its
columns.

                        test:

                            first run, rows=2, columns=5 , start = 15

                                    ((2 - 2) * 5) + 15 = 0*5 + 15 = 15 :
correct, first row
                                    ((2 - 1) * 5) + 15 = 1*5 + 15 = 20 :
correct, second row
                                    ((2 - 0) * 5) + 15 = 2*5 + 15 = 25 :
correct, third row, if there were any


                -->
            <xsl:call-template name="displayCells">
                <xsl:with-param name="dc_start" select="$start + (($rows -
$dr_rows) * $dr_columns)"/>
                <xsl:with-param name="dc_columns" select="$dr_columns"/>
                <xsl:with-param name="dc_current_row" select="$rows -
$dr_rows"/>
            </xsl:call-template>

        </tr>

<xsl:comment>end of row</xsl:comment>

        <!-- next row -->
        <xsl:call-template name="displayRows">
            <xsl:with-param name="dr_start" select="$start + (($rows -
$dr_rows) * $dr_columns)"/>
            <xsl:with-param name="dr_columns" select="$dr_columns"/>
            <xsl:with-param name="dr_rows" select="$dr_rows - 1"/>
        </xsl:call-template>

    </xsl:if>
</xsl:template>

<xsl:template name="displayCells">
    <!-- the position of the image to be displayed in this cell -->
    <xsl:param name="dc_start"/>
    <!-- the number of columns to be displayed -->
    <xsl:param name="dc_columns"/>
    <!-- the number of the current row, for convenience -->
    <xsl:param name="dc_current_row"/>

    <xsl:if test="$dc_columns > 0">
        <xsl:variable name="CELL" select="IMAGES/IMAGE[$dc_start]"/>
        <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>

        <!-- next cell -->
        <xsl:call-template name="displayCells">
               <xsl:with-param name="dc_start" select="$dc_start + 1"/>
               <xsl:with-param name="dc_columns" select="$dc_columns - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>




 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]