This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: a new(?) grouping problem
- To: "XSL List" <xsl-list at mulberrytech dot com>
- Subject: Re: a new(?) grouping problem
- From: "Nikolai Grigoriev" <grig at renderx dot com>
- Date: Thu, 29 Jun 2000 19:34:21 +0400
- References: <200006290102.TAA21878@skew.org>
- Reply-To: xsl-list at mulberrytech dot com
Mike,
> I want to group consecutive days with the same hours together, and just
> print the first and last day in each group.
The same solution as posted before, but slightly shorter ;-)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0"/>
<!-- Root template: just create a table. -->
<xsl:template match="Hours">
<table><xsl:apply-templates/></table>
</xsl:template>
<!-- Exclude holidays from processing -->
<xsl:template match="Holidays" priority="2"/>
<!-- Single days, except for holidays. -->
<!-- A modeless template creates the row -->
<xsl:template match="Hours/*">
<xsl:variable name="hours" select="text()"/>
<xsl:if
test="not(preceding-sibling::*[not(self::Holidays)][1][text()=$hours])">
<tr>
<td>
<xsl:value-of select="name()"/>
<xsl:apply-templates mode="end"
select="following-sibling::*[not(self::Holidays)][1][text()=$hours]"/>
</td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:if>
</xsl:template>
<!-- A day closes the period if there's no better candidate -->
<xsl:template match="Hours/*" mode="end">
<xsl:variable name="hours" select="text()"/>
<xsl:choose>
<xsl:when
test="following-sibling::*[not(self::Holidays)][1][text()=$hours]">
<xsl:apply-templates mode="end"
select="following-sibling::*[not(self::Holidays)][1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:text> - </xsl:text><xsl:value-of select="name()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list