This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: need further assistance with grouping nodes into groups of 3
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: [xsl] Re: need further assistance with grouping nodes into groups of 3
- From: "Yang" <sfyang at unisvr dot net dot tw>
- Date: Sun, 30 Sep 2001 10:55:29 +0800
- Reply-To: xsl-list at lists dot mulberrytech dot com
Chuck,
Your problem is of typical group by position. You can get good
reference from Jeni's site, dpawson site, or this xslt post.
A fine tuned shorter and generic Dimitre version (tableH-template.xsl) is
used for your reference.
Changing param numCols and source is only job you have to do.
Besides, you have to rewrite template with mode="normal" for more
specific information.
The code also allows alternatively coloring on the changing group.
Enjoy it.
*** xslt list ***
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:include href="tableH-template.xsl"/>
<xsl:param name="numCols" select="3" />
<xsl:variable name="source" select="/images/image" />
<xsl:template match="/">
<xsl:apply-templates select="$source" mode="multiColumn">
<xsl:with-param name="nodes" select="$source"/>
<xsl:with-param name="numCols" select="$numCols"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="normal">
<td>
Path: <a href="{thumbimage/thumbpath}">
<xsl:value-of select="thumbimage/thumbformat"/></a><br/>
<xsl:for-each select="*[name()!='thumbimage']">
<xsl:value-of select="concat(name(),':', substring(.,1,25))"/><br/>
</xsl:for-each>
</td>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="*" mode="multiColumn">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="numCols" select="1"/>
<xsl:variable name="vCurPosition" select="position()"/>
<!-- output the table row when test =1 -->
<xsl:if test="$vCurPosition mod $numCols = 1">
<table>
<!-- alternative coloring for odd/even row -->
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="$vCurPosition mod (2 * $numCols) =
1">aqua</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$color}">
<xsl:apply-templates mode="normal"
select="$nodes
[position() >= $vCurPosition
and
position() < $vCurPosition +
$numCols]"/>
</tr>
</table>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="normal">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
Sun-fu Yang
sfyang@unisvr.net.tw
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list