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.
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).
<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>
<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.
<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>
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.
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:
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.
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?
Discussion Questions: How is this done now? Why is this problematic? What is needed on the server side with this newer approach?
Discussion Questions: What is the problem? How would this model change that? (hint: think about a Flash-only site)
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?
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.
Last modified: 10 Mar 2008 10:40:04 AM