sanjeewa
sanjeewa

Reputation: 604

Convert Date from DD-MMM-YYYY to YYYY-MMM-DD using xslt 2.0

I have a XML that has a date formatted as (2019-Aug-19) (YYYY-MMM-DD).

I want this to be transformed to (2019-08-19) (YYYY-MM-DD)

Hear is a sample what i have done. But no luck

<xsl:value-of select="format-dateTime(xs:dateTime(concat(date, T00:00:00Z')), '[D01]-[M01]-[Y0001]')"/>

Upvotes: 0

Views: 730

Answers (1)

Amrendra Kumar
Amrendra Kumar

Reputation: 1816

Here you go may be another simple method:

XML:

<time>2019-Aug-19T00:00:00Z</time>

XSLT:

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

    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:variable name="dt" select="time"/>
    <xsl:template match="/">
      <xsl:value-of select="format-dateTime(xs:dateTime(replace($dt, substring-before(substring-after($dt,'-'),'-') ,format-number(index-of(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov', 'dec'),lower-case(substring-before(substring-after(lower-case($dt),'-'), '-'))),'00'))),'[D01]-[M01]-[Y0001]')"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

Result:

19-08-2019

For YYYY-MM-DD you can do:

<xsl:value-of select="format-dateTime(xs:dateTime(replace($dt, substring-before(substring-after($dt,'-'),'-') ,format-number(index-of(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov', 'dec'),lower-case(substring-before(substring-after(lower-case($dt),'-'), '-'))),'00'))),'[Y0001]-[M01]-[D01]')"/>

Result:

2019-08-19

See Link: http://xsltransform.net/nbiCsZm

Upvotes: 1

Related Questions