Formatting dates with XSL

Posted: 20 August 2009 in Components, FormsPublisher, Pages, Presentation Template, SitePublisher, Sites, XSL, XSL presentation templates

When you have a date field or any other date represented as strings and want to re-format them in a different format, it is still feasible to do so using XSL.

As an example, take the date “2009-08-14T00:00:00+09:00”. This is not very user friendly but “14 August 2009 “would be a lot more readable. Let’s start with a sample XML document to transform into HTML using XSL that we’ll call articles.xml.

<?xml version="1.0" ?>
<articles>
    <article>
        <title>Lorem ipsum dolor sit amet</articles>
        <description>Morbi elit quam, porttitor non aliquet ultrices, elementum quis erat. Nulla tempus lorem eget tortor accumsan cursus. Fusce quis massa laoreet mi suscipit rutrum. Duis id tincidunt eros. Aenean feugiat consectetur ligula vitae facilisis. Morbi dapibus velit eu mauris rutrum ut ornare lorem tempor. Aenean ut sapien a turpis imperdiet laoreet. Fusce iaculis, eros at ultricies vehicula, dui velit condimentum eros, a suscipit urna justo sed augue. Phasellus venenatis varius feugiat. Aliquam nec orci leo.</description>
        <date>2009-08-14T00:00:00+09:00</date>
    </article>
</articles>

Let’s create the first version of the XSL and we’ll save it in articles.xsl.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="html"/>

 <xsl:template match="/">
 <html>
 <head>
 <title>newstylesheet.xsl</title>
 </head>
 <body>
 </body>
 </html>
 </xsl:template>

</xsl:stylesheet>

We can extract portions of the date uisng the substr function for day, month and year and we can use the call-templates functionality to display the proper month names (e.g. January) by passing the month number (e.g. 01) as a parameter to the template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="html"/>

 <xsl:template match="/articles">
 <html>
 <head>
 <title>newstylesheet.xsl</title>
 </head>
 <body>
 <xsl:apply-templates select="article" />
 </body>
 </html>
 </xsl:template>

<xsl:template match="article">
 <xsl:value-of select="title" /> (<xsl:apply-templates select="date" />)
</xsl:template>

<xsl:template match="date">

 <!--
 year: <xsl:value-of select="substring(.,1,4)" /><br />
 month:<xsl:value-of select="substring(.,6,2)" /><br />
 day: <xsl:value-of select="substring(.,9,2)" /><br />
 -->

 <xsl:value-of select="substring(.,9,2)" />
 <!-- replacing a month number by its month name -->
 <xsl:call-template name="number-to-month">
 <xsl:with-param name="month"><xsl:value-of select="substring(.,6,2)" /></xsl:with-param>
 </xsl:template>
 <xsl:value-of select="substring(.,1,4)" />
</xsl:template>

<!-- replaces a month number by its month name -->
<xsl:template name="number-to-month">
 <xsl:param name="month"/>
 <xsl:choose>
 <xsl:when test="$month = '01'">January</xsl:when>
 <xsl:when test="$month = '02'">February</xsl:when>
 <xsl:when test="$month = '03'">March</xsl:when>
 <xsl:when test="$month = '04'">April</xsl:when>
 <xsl:when test="$month = '05'">May</xsl:when>
 <xsl:when test="$month = '06'">June</xsl:when>
 <xsl:when test="$month = '07'">July</xsl:when>
 <xsl:when test="$month = '08'">August</xsl:when>
 <xsl:when test="$month = '09'">September</xsl:when>
 <xsl:when test="$month = '10'">October</xsl:when>
 <xsl:when test="$month = '11'">November</xsl:when>
 <xsl:when test="$month = '12'">December</xsl:when>
 </xsl:choose>
 </xsl:template>

</xsl:stylesheet>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s