XSLT: convert ISO 8601 DateTime format

Today I did a little bit of XSLT coding and stumbled over the problem to convert ISO 8601 date strings into a more human readable format.

Specifically I wanted to turn a date of the form 2008-04-10T16:12:27Z to the typical german format of 10.04.2008 16:12:27 and spit the whole thing out as HTML to display it on a webpage.
After a bit of time I came up with the following XSLT (sanitized):

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html"/>
        <xsl:template match="/">
                <xsl:element name="div">
                <xsl:element name="abbr">
                        <xsl:attribute name="title">
                                <xsl:value-of select="create_dt"/>
                                <xsl:value-of select="’Z’"/>
                        </xsl:attribute>
                        <xsl:attribute name="class">dtstart</xsl:attribute>
                        <xsl:call-template name="FormatDate">
                                <xsl:with-param name="DateTime" select="create_dt"/>
                        </xsl:call-template>
                </xsl:element>
                </xsl:element>
        </xsl:template>
        <xsl:template name="FormatDate">
        <xsl:param name="DateTime" />
        <xsl:variable name="mo">
                        <xsl:value-of select="substring($DateTime,6,2)" />
        </xsl:variable>
        <xsl:variable name="day">
                        <xsl:value-of select="substring($DateTime,9,2)" />
          </xsl:variable>
        <xsl:variable name="year">
                        <xsl:value-of select="substring($DateTime,1,4)" />
        </xsl:variable>
        <xsl:variable name="time">
                        <xsl:value-of select="substring($DateTime,12,8)" />
        </xsl:variable>
        <xsl:variable name="hh">
                        <xsl:value-of select="substring($time,1,2)" />
        </xsl:variable>
        <xsl:variable name="mm">
                        <xsl:value-of select="substring($time,4,2)" />
        </xsl:variable>
        <xsl:variable name="ss">
                        <xsl:value-of select="substring($time,7,2)" />
        </xsl:variable>
                <xsl:value-of select="$day"/>
                <xsl:value-of select="’.’"/>
                <xsl:value-of select="$mo"/>
                <xsl:value-of select="’.’"/>
                <xsl:value-of select="$year"/>
                <xsl:value-of select="’ ‘"/>
                <xsl:value-of select="$hh"/>
                <xsl:value-of select="':’"/>
                <xsl:value-of select="$mm"/>
                <xsl:value-of select="':’"/>
                <xsl:value-of select="$ss"/>
        </xsl:template>
</xsl:stylesheet>
 

Using this example and the mighty power of substring() it should be pretty easy to convert any date format into an other one.