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

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

10-JAN-2013 TO 20130110

in XSLT 1.0

+4
source share
3 answers

It is very simple with the xsl:choose element and does not require any extensions.

This style sheet

 <?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> 

Applies to 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 conclusion

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

If you can use node -set as an extension of 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 an additional question on the command:

You can call the template anywhere you 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 a new variable and use it.

 <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> 
+1
source

I understand that this topic is quite outdated, but I decided to share my template if someone wants to save some time by copying and pasting it into the kit. Thanks to the answers above as they are based on them. My template is simply converted 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> 
0
source

Source: https://habr.com/ru/post/1480127/


All Articles