Re: [xsl] Grouping with XSLT

Subject: Re: [xsl] Grouping with XSLT
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 31 Oct 2001 19:00:38 +0000
Hi Michael,

> and need to generate some sort of table (diary style) where I list
> the hours from 9-5 in a style like
> 9:00
> 9:15
> 9:30
> 9:45
> 10:00
> ...

Since you can never predict which hours are going to actually be
involved in an activity, I think you'd be safer generating these times
using a recursive template. To create a heading for every hour, you
could use something like:

<xsl:template name="createDiary">
  <xsl:param name="hour" select="9" />
  <h2><xsl:value-of select="format-number($hour, '00')" />:00</h2>
  ...
  <xsl:if test="$hour &lt; 18">
    <xsl:call-template name="createDiary">
      <xsl:with-param name="hour" select="$hour + 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Since you're searching the date elements time and time again, it's
probably best to set up a key that indexes each date element by its
starth, as follows:

<xsl:key name="dates" match="date" use="number(starth)" />

Then you can retrieve all the date elements with a particular start
hour with:

  key('dates', $hour)

and iterate over them within the createDiary template, as follows:

<xsl:template name="createDiary">
  <xsl:param name="hour" select="9" />
  <h2><xsl:value-of select="format-number($hour, '00')" />:00</h2>
  <xsl:for-each select="key('dates', $hour)">
    <xsl:sort select="startm" data-type="number" />
    <p>
      Start: <xsl:value-of select="concat(starth, ':', startm)" />,
      <xsl:value-of select="activity" />
    </p>
  </xsl:for-each>
  <xsl:if test="$hour &lt; 18">
    <xsl:call-template name="createDiary">
      <xsl:with-param name="hour" select="$hour + 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread