0. Overview

We shall look at two things today: dates and times in XSL-T, and client-side transformations using JavaScript and XSL-T. The first item is our "details of the day" subject, and the second is our "what do you think of this idea?" subject.

1. Dates & Times

While XSLT (v 1) has no facilities to directly deal with dates and times, this is none-the-less an everyday requirement of manipulating data. The first part of the problem of course is to pass a date into a template (thanks to the XSLT Cookbook for the code examples which were modied to fit this lecture material).

Passing into a Template

ISO Formatted Dates

<xsl:call-template name="calc-day-of-week">
  <xsl:with-param name="date" select="'2002-01-01T01:00:00'"/>
</xsl:call-template>

<xsl:call-template name="calc-day-of-week">
  <xsl:with-param name="date" select="'2002-01-01'"/>
</xsl:call-template>

Date Components Stored Separately

<xsl:call-template name="calc-day-of-week">
  <xsl:with-param name="year" select="2002"/>
  <xsl:with-param name="month" select="01"/>
  <xsl:with-param name="day" select="01"/>
</xsl:call-template>

Note: this is based on using the Gregorian calendar.

Overriding ISO Date String Formatting

<xsl:call-template name="calc-day-of-week">
  <xsl:with-param name="date" select="'2002-01-01'"/>
  <xsl:with-param name="day" select="25"/>
</xsl:call-template>

Figuring Day of the Week

You will often be able to determine the day and month and year, but will need to determine the day of the week (Monday, Tuesday, etc).

<xsl:template name="calc-day-of-week">
  <xsl:param name="date-time"/>
  <xsl:param name="date" select="substring-before( $date-time, 'T' )"/>
  <xsl:param name="year" select="substring-before( $date, '-' )"/>
  <xsl:param name="month" select="substring-before( substring-after( $date, '-' ), '-' )"/>
  <xsl:param name="day" select="substring-after( substring-after( $date, '-' ),  '-' )"/>
  <xsl:variable name="a" select="floor( ( 14 - $month ) div 12 )"/>
  <xsl:variable name="y" select="$year - $a"/>
  <xsl:variable name="m" select="$month + 12 * $a - 2"/>
  <xsl:value-of select="( $day + $y + floor( $y div 4 ) - floor( $y div 100 ) + floor( $y div 400 ) + 
                                                  floor( ( 31 * $m) div 12 ) ) mod 7"
</xsl:template>

Note the extensive use of floor to emulate integer arithmetic. XSLT does numbers as floating-point internally, so we have to do these dances.

2. Client-Side XSL-T Using JavaScript

Two approaches are emerging to making on-demand XSL-T application against XML data happen on the client-side, instead of on the server-side as is current practice. These are:

  1. direct support for XSL-T by the browser itself (see this article and this article as well)
  2. XSL-T support in JavaScript (see this article)

Why Should We Care?

There are a number of reasons to care about whether the on-demand application of XSL-T to XML happens on the client-side or the server-side. The issue of separation of concerns is an old one in computer science, the problem of needing specialized server(s) to accomplish these simple tasks is one which frequently makes website implementors somewhat insane, and the difficulties in maintaining control over presentation when adds/changes/deletes must happen through the agency of another person (for which read, sysadmin) are well-known.

Separation of Concerns

Discussion Questions: Why is it important to separate concerns? What concerns should be separated from XML data (hint hint)? How do most current XML/XSLT server setups violate separation of concerns? What about content management systems?

Elimination of Need for Specialized Server(s)

Discussion Questions: How is this done now? Why is this problematic? What is needed on the server side with this newer approach?

Increase in Web-site Implementor Control

Discussion Questions: What is the problem? How would this model change that? (hint: think about a Flash-only site)

Question: Which is "Better"?

Discussion Questions: Is there an absolute answer to this? What are the advantages of using built-in browser support versus supplying a library of JavaScript functions?

Working Example

Take a look at this page, and try out some of the XML and XSL-T files in this directory to see what works and doesn't work in the demo tool. All files referred to are public, so you could download the JavaScript and the HTML to your own account on some other server. Thus, the security model (which only allows loading of files from the same server as the HTML came from) will not be violated.


revalidate XHTML Revalidate CSS Section 508 testing unique visitor counter

Last modified: 9 Mar 2009 11:02:44 AM