[xsl] Re: Re: loop in creation of table

Subject: [xsl] Re: Re: loop in creation of table
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 27 Sep 2001 05:38:21 -0700 (PDT)
> Thanks for your kindness to clean up my xslt redundant codes.
> To make this piece of template  be more generic,
> a variable **source** is to define the  xml-dependent tree structure.
> So the template can be used without further touching, i.e.,
>   <xsl:variable name="source" select="/parent-of-subsystem_id/subsystem_id"
> />
> and use it as the value of select attribute.  Meantimes the element abbr *
> is used
> with match. The test seems ok.  But I wonder what might be missing from this
> kind of approach.

Thanks a lot, Sung Fu.

The code seems good to me.

Just a final thing:

            <xsl:apply-templates mode="normal"
                 select="$nodes[position() >= $vCurPosition
                     and (position() - $vCurPosition) mod $numCols = 0]" />

in our case is equivalent to:

            <xsl:apply-templates mode="normal"
                 select="$nodes[(position() - $vCurPosition) mod $numCols = 0]" />

and the latter is shorter and more efficient.

So the final version looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output method="html"/>

    <xsl:param name="numCols" select="4" />
    <xsl:variable name="source"

    <xsl:template match="/">
            <xsl:apply-templates mode="multiColumn"
                                 select="$source[position() &lt;=$numCols]">
                <xsl:with-param name="numCols" select="$numCols" />
                <xsl:with-param name="nodes" select="$source" />

    <xsl:template mode="multiColumn" match="*">
        <xsl:param name="numCols" select="1" />
        <xsl:param name="nodes" select="/.." />

        <xsl:variable name="vCurPosition" select="position()" />

        <xsl:variable name="vColour">
                <xsl:when test="$vCurPosition mod 2 = 1">aqua</xsl:when>

        <tr bgcolor="{$vColour}">
            <xsl:apply-templates mode="normal"
                 select="$nodes[(position() - $vCurPosition) mod $numCols = 0]" />

    <xsl:template match="*" mode="normal">
            <xsl:value-of select="." />

Dimitre Novatchev.

Do You Yahoo!?
Listen to your Yahoo! Mail messages from any phone.

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

Current Thread