sum
sum

Reputation: 373

Convert date from DD-MMM-YYYY to YYYYMMDD format in xslt 1.0

How we can convert the date format from DD-MMM-YYYY to YYYY-MM-DD in XSLT.

10-JAN-2013 TO 20130110

in XSLT 1.0

Upvotes: 6

Views: 37658

Answers (3)

Craig B
Craig B

Reputation: 333

I realise this thread is quite out of date now, but thought I'd share my template in case someone wanted to save some time by copying and pasting it. Thanks to the answers above as this is based on them. My template simply converts from dd/MM/yyyy to yyyy-MM-dd; and also from dd/MM/yyyy HH:mm:ss to yyyy-MM-ddTHH:mm:ss.

    <!--
  Template Name: Date
  Description: Takes a date in the format dd/MM/yyyy and outputs it in the format yyyy-mm-dd
               Additionally will take a datetime in the format dd/MM/yyyy HH:mm:ss and output in the format yyyy-MM-ddTHH:mm:ss
  -->
  <xsl:template name="date">
    <xsl:param name="slashFormattedDate"/>
    <xsl:param name="hasTime"/>
    <xsl:variable name="dd" select="substring-before($slashFormattedDate, '/')"/>
    <xsl:variable name="monthYear" select="substring-after($slashFormattedDate, '/')"/>
    <xsl:variable name="mm" select="substring-before($monthYear, '/')"/>
    <xsl:variable name="yyyyTemp" select="substring-after($monthYear, '/')"/>
    <xsl:variable name="yyyy">
      <xsl:choose>
        <xsl:when test="$hasTime='Y'">
          <xsl:value-of select="substring-before($yyyyTemp, ' ')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$yyyyTemp"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$hasTime='Y'">
        <xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>T<xsl:value-of select="substring-after($yyyyTemp, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$yyyy"/>-<xsl:value-of select="$mm"/>-<xsl:value-of select="$dd"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Upvotes: 1

Borodin
Borodin

Reputation: 126742

This is very straightforward with an xsl:choose element, and doesn't require any extensions.

This stylesheet

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/root">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="date">
    <xsl:copy>
      <xsl:call-template name="date">
        <xsl:with-param name="dd-mmm-yyyy" select="."/>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="date">
    <xsl:param name="dd-mmm-yyyy"/>
    <xsl:variable name="dd" select="substring-before($dd-mmm-yyyy, '-')"/>
    <xsl:variable name="mmm-yyyy" select="substring-after($dd-mmm-yyyy, '-')"/>
    <xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
    <xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>
    <xsl:value-of select="$yyyy"/>
    <xsl:choose>
      <xsl:when test="$mmm = 'JAN'">01</xsl:when>
      <xsl:when test="$mmm = 'FEB'">02</xsl:when>
      <xsl:when test="$mmm = 'MAR'">03</xsl:when>
      <xsl:when test="$mmm = 'APR'">04</xsl:when>
      <xsl:when test="$mmm = 'MAY'">05</xsl:when>
      <xsl:when test="$mmm = 'JUN'">06</xsl:when>
      <xsl:when test="$mmm = 'JUL'">07</xsl:when>
      <xsl:when test="$mmm = 'AUG'">08</xsl:when>
      <xsl:when test="$mmm = 'SEP'">09</xsl:when>
      <xsl:when test="$mmm = 'OCT'">10</xsl:when>
      <xsl:when test="$mmm = 'NOV'">11</xsl:when>
      <xsl:when test="$mmm = 'DEC'">12</xsl:when>
    </xsl:choose>
    <xsl:value-of select="$dd"/>
  </xsl:template>

</xsl:stylesheet>

Applied to this XML data

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <date>10-JAN-2013</date>
  <date>04-JUL-1776</date>
  <date>31-DEC-1999</date>
</root>

Produces this output

<?xml version="1.0" encoding="utf-8"?>
<root>
   <date>20130110</date>
   <date>17760704</date>
   <date>19991231</date>
</root>

Upvotes: 9

hr_117
hr_117

Reputation: 9627

If you can use node-set as extension to your xslt 1.0 processor you can try this.

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">

<xsl:output method="xml" indent="yes" />

<xsl:variable name="date" select="'10-JAN-2013'" />

<xsl:variable name="month_data_tmp">
    <month short="JAN" nr="01" />
    <!--- and so on for each month -->
</xsl:variable>
<xsl:variable name="month_data" select="exsl:node-set($month_data_tmp)" />


<xsl:template name="format_date" >
    <xsl:param name ="date" />
    <xsl:variable name ="day" select="substring-before($date, '-')" />
    <xsl:variable name ="month_and_year" select="substring-after($date, '-')" />
    <xsl:variable name ="year" select="substring-after($month_and_year, '-')" />
    <xsl:variable name ="month" select="substring-before($month_and_year, '-')" />
    <xsl:value-of select="$year"/>
    <xsl:value-of select="$month_data/month[@short=$month]/@nr"/>
    <xsl:value-of select="$day"/>
</xsl:template>

<xsl:template match="/" >
    <xsl:call-template name="format_date" >
        <xsl:with-param name ="date" select="$date"/>
    </xsl:call-template>
</xsl:template>

The output will be:

20130110

Update doe to additional question in command:

You can call the template at any place where you have used <xsl:value-of select="$date"/>before.

<result>
    <xsl:call-template name="format_date" >
        <xsl:with-param name ="date" select="$date"/>
    </xsl:call-template>
</result>

Or you can assign the result to an new variable and use this.

<xsl:variable name="newdate">
    <xsl:call-template name="format_date" >
        <xsl:with-param name ="date" select="$date"/>
    </xsl:call-template>
</xsl:variable>

<result>
    <xsl:value-of select="$newdate"/>
</result>

Upvotes: 1

Related Questions