Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xslt

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

like image 648
sum Avatar asked May 10 '13 14:05

sum


People also ask

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit) # (Digit, zero shows as absent)

What is text () in XSLT?

The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What is XSLT template match?

XSLT <xsl:template> The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).


2 Answers

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>
like image 120
Borodin Avatar answered Nov 15 '22 11:11

Borodin


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>
like image 42
hr_117 Avatar answered Nov 15 '22 11:11

hr_117