[xsl] Re: Template repository

Subject: [xsl] Re: Template repository
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Thu, 15 Mar 2001 16:17:26 +0100
> There are groups of them:
>  - date/time/calendar

Something out of this category: Calculations of
day in week, day in year and week in year (ISO-like)
from y-m-d values. Be aware: week 0 (zero) ist actually
the last week of the previous year. This code is good
enough when comparing whether two days are in the
same week, if you want to print out the ISO week/year
you must further process the week 0 case.

The template matches an
<!ELEMENT date (year,month,day)>
year, month, day being #PCDATA

Warning: No Warranty! No error checking whatsoever! Use
this at your own risk!

Questions rised:
- Who wants to host this snippet? Mr.Pawson?
- Policy of posting long snippets to this list?
- Coding style? Indentations, names, parameters vs. global
  variables/context nodes, comments, how to return return
  values etc.
- Spec of source XML (DTD, Schema, verbal...), if applicable.
- Documentation style (uh, DocBook :-)?
- Copyright policy? I wonder how e.g. LGPL should be interpreted
  in this context...

Regards
J.Pietschmann

  <xsl:template match="date">
    <!-- Auxillary variable. -->
    <xsl:variable name="offset">
      <xsl:choose>
        <xsl:when test="month &lt; 3">0</xsl:when>
        <xsl:when test="(year mod 4)=0
                  and not((year mod 100)=0
                  and (year mod 400)!=0)">-1</xsl:when>
        <xsl:otherwise>-2</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Another auxillary variable. -->
    <xsl:variable name="prev-year" select="year - 1"/>
    <!-- Day of week of Jan 1st, this year. -->
    <xsl:variable name="weekday1" select="(365 * $prev-year
                          + floor($prev-year div 4)
                          - floor($prev-year div 100)
                          + floor($prev-year div 400)
                          + 1) mod 7"/>
    <!-- Number of day in year.
         Range: 1-365 (ordinary years) 1-366 (leap years) -->
    <xsl:variable name="days" select="floor(((367 * month) - 362) div 12)
                          + $offset
                          + day"/>
    <!-- Number of day in week. 1=Monday, 2=Tuesday, ... 7=Sunday -->
    <xsl:variable name="weekday">
      <xsl:choose>
        <xsl:when test="(($weekday1 + $days - 1) mod 7)=0">7</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="($weekday1 + $days - 1) mod 7"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Number of week in year.
         Range: 0-53. Zero means actually the last week of the previous year.
         Week 1 is the week which contains the first thursday in the year. -->
    <xsl:variable name="week">
      <xsl:choose>
        <xsl:when test="$weekday1 &lt; 5">
          <xsl:value-of select="floor(($weekday1 + 5 + $days) div 7)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="floor(($weekday1 - 2 + $days) div 7)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:text>Date: </xsl:text>
    <xsl:value-of select="day"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="month"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="year"/>
    <xsl:text>  </xsl:text>
    <xsl:text>Day of week: </xsl:text>
    <xsl:value-of select="$weekday"/>
    <xsl:text>  </xsl:text>
    <xsl:text>Day in year: </xsl:text>
    <xsl:value-of select="$days"/>
    <xsl:text>  </xsl:text>
    <xsl:text>Week: </xsl:text>
    <xsl:value-of select="$week"/>
    <xsl:text>&#xD;&#xA;</xsl:text>

  </xsl:template>

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


Current Thread