Re: Building a calendar

Subject: Re: Building a calendar
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 01 Sep 1999 12:50:36 -0400
At 99/09/01 10:21 -0400, Richard Lander wrote:
I'd like to build a calendar, a table really, by processing a set of events. My
mind keeps on thinking of loops, which I'm pretty sure XSLT lacks. Outside of
iteratively building the calendar within my stylesheet and checking for events
on each day, I can't think of a good way of tranforming few events into many
table cells. Hmm.

That's the way I thought of doing it.


I have looked at Oren Ben-Kiki's queens.xsl to see if I could find
any other ideas. I see that he uses recursion to build his tables.

That's the way to do "loops" ... because there are no global (varying) variables, one must walk through a template as many times as desired.


It seemed to work for me ... an example is below using XT-19990822.

I hope this helps.

........ Ken

p.s. since there is ambiguity between Tuesday/Thursday and Saturday/Sunday using a letter for the day of the week, I'm using a number to start the month with Sunday="1"

p.p.s. I didn't include the generated HTML because it screws up mailers


T:\lander>type test.xml <?xml version="1.0"?> <CALENDAR> <MONTH start="4" end="30"> <!-- start is the day of the week that the month starts. Wednesday in this case --> <EVENT date="4"> <PARA>Some event description</PARA> </EVENT> <EVENT date="24"> <PARA>another event description</PARA> </EVENT> <!--Lots of time for vacation this month! --> </MONTH> <MONTH start="6" end="31"> <EVENT date="5"> <PARA>Some event description</PARA> <PARA>Second para of description</PARA> </EVENT> <EVENT date="17"> <PARA>first event description</PARA> </EVENT> <EVENT date="17"> <PARA>second event description</PARA> </EVENT> </MONTH> <!--May be more MONTHs --> </CALENDAR> T:\lander>type test.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<xsl:output method="html"/>

<xsl:template match="/">                   <!--root rule-->
  <xsl:apply-templates select="CALENDAR"/> <!--restrict instances-->
</xsl:template>

<xsl:template match="CALENDAR"> <!--each month belongs in a table-->
  <html>
    <xsl:for-each select="MONTH">
      <table border="1" valign="top">
        <xsl:call-template name="doweek">
          <xsl:with-param name="start" select="@start - 1"/>
          <xsl:with-param name="end" select="@end"/>
        </xsl:call-template>
      </table>
    </xsl:for-each>
  </html>
</xsl:template>

<xsl:template name="doweek">    <!--each week belongs in a row-->
  <xsl:param name="start" select="0"/>
  <xsl:param name="end"/>
  <xsl:param name="day" select="1"/>
  <xsl:if test="$day &lt;= $end">
    <tr valign="top">
      <xsl:call-template name="doday">
        <xsl:with-param name="day" select="$day"/>
        <xsl:with-param name="start" select="$start"/>
        <xsl:with-param name="end" select="$end"/>
      </xsl:call-template>
    </tr>
    <xsl:call-template name="doweek">
      <xsl:with-param name="day" select="$day+7-$start"/>
      <xsl:with-param name="end" select="$end"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="doday">     <!--each day belongs in a column-->
  <xsl:param name="start" select="0"/> <!--only def'd first time-->
  <xsl:param name="end"/>
  <xsl:param name="day"/>
  <xsl:param name="weekday" select="7"/>
  <xsl:choose>
    <xsl:when test="$start>0">  <!--not started days yet-->
      <td>
      </td>
      <xsl:call-template name="doday">
        <xsl:with-param name="start" select="$start - 1"/>
        <xsl:with-param name="end" select="$end"/>
        <xsl:with-param name="day" select="$day"/>
        <xsl:with-param name="weekday" select="$weekday - 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>             <!--a typical day of the week-->
      <td>
        <b><xsl:value-of select="substring('SFTWTMS', $weekday, 1)"
         /><xsl:text> </xsl:text><xsl:value-of select="$day"/>:</b><br/>
        <xsl:for-each select=".//EVENT[@date=$day]">
          <p><i><xsl:value-of select="position()"/>: </i>
             <xsl:value-of select="PARA[1]"/></p>
          <xsl:for-each select="PARA[position()>1]">
            <p><xsl:value-of select="."/></p>
          </xsl:for-each>
        </xsl:for-each>
      </td>
      <xsl:if test="$weekday > 1 and $day &lt; $end">
        <xsl:call-template name="doday">
          <xsl:with-param name="end" select="$end"/>
          <xsl:with-param name="day" select="$day+1"/>
          <xsl:with-param name="weekday" select="$weekday - 1"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

T:\lander>call xsl test.xml test.xsl test.htm
T:\lander>dir test.htm

 Volume in drive T has no label
 Volume Serial Number is 3550-B22A
 Directory of T:\lander

TEST     HTM         2 554  99-09-01  12:49 test.htm
         1 file(s)          2 554 bytes
         0 dir(s)     196 673 536 bytes free

T:\lander>

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:                     MT'99 1999-12-05/06


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



Current Thread