Monday, February 6, 2012

Date conversion function in XSLT

Recently, i found a need to convert one format to different date formats using XSLT script..This is a sample script i wrote to convert      Thu Jun 07 18:00:00 EDT 2012 --to--> 2012-06-07T18:00:00  which may be useful to others also...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
    <xsl:template match="/">
   
        <xsl:element name="PickUpDateTime">
            <xsl:call-template name="FormatDate">
                <xsl:with-param name="DateTime" select="//date" />
            </xsl:call-template>
        </xsl:element>
   
    </xsl:template>
   
    <xsl:template name="FormatDate">
        <xsl:param name="DateTime" />   
        <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
        <xsl:variable name="day">
            <xsl:value-of select="substring($DateTime,1,3)" />
        </xsl:variable>
       
        <xsl:variable name="month-temp">
            <xsl:value-of select="substring-after($DateTime,' ')" />
        </xsl:variable>       
        <xsl:variable name="month">
            <xsl:value-of select="substring($month-temp,1,3)" />
        </xsl:variable>       
       
        <xsl:variable name="date-temp">
            <xsl:value-of select="substring-after($month-temp,' ')" />
        </xsl:variable>
        <xsl:variable name="date">
            <xsl:value-of select="substring($date-temp,1,2)" />
        </xsl:variable>       
       
        <xsl:variable name="year">
            <xsl:value-of select="substring-after($DateTime,'EDT ')" />
        </xsl:variable>           
       
        <xsl:variable name="time">
            <xsl:value-of select="substring-after($date-temp,' ')" />
        </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="$year" />

        <xsl:value-of select="'-'" />

        <xsl:choose>
            <xsl:when test="$month = 'Jan'">01</xsl:when>
            <xsl:when test="$month= 'Feb'">02</xsl:when>
            <xsl:when test="$month = 'Mar'">03</xsl:when>
            <xsl:when test="$month = 'Apr'">04</xsl:when>
            <xsl:when test="$month = 'May'">05</xsl:when>
            <xsl:when test="$month = 'Jun'">06</xsl:when>
            <xsl:when test="$month = 'Jul'">07</xsl:when>
            <xsl:when test="$month = 'Aug'">08</xsl:when>
            <xsl:when test="$month = 'Sep'">09</xsl:when>
            <xsl:when test="$month = 'Oct'">10</xsl:when>
            <xsl:when test="$month = 'Nov'">11</xsl:when>
            <xsl:when test="$month = 'Dec'">12</xsl:when>
        </xsl:choose>

        <xsl:value-of select="'-'" />
   
        <xsl:value-of select="$date" />
        <xsl:value-of select="'T'" />
        <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>
 

No comments:

Post a Comment