Reputation: 75
I have a requirement where the date format needs to be changed from yyyy-mm-dd
to dd-mmm-yyyy
using XSL.
I've managed to get the value of the date and written the logic to change it. But somehow the value is not getting changed.
The request and XSL can also be found here: Change the Date format
XML Input
<params>
<param name ="query" >
<queryData>
<parameter index ="0" value ="2017-12-06" dataType="java.lang.String"/>
<parameter index ="1" value ="2017-12-03" dataType="java.lang.String"/>
</queryData>
</param>
</params>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="params/param/queryData/parameter[@value='*'][normalize-space()]">
<xsl:copy>
<xsl:call-template name="reformat-date">
<xsl:with-param name="date" select="." />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="reformat-date">
<xsl:param name="date" />
<xsl:variable name="dd" select="substring-after(substring-after($date, '-'), '-')" />
<xsl:variable name="mmm" select="substring-before(substring-after($date, '-'), '-')" />
<xsl:variable name="yyyy" select="substring-before($date, '-')" />
<xsl:value-of select="$dd" />
<xsl:text>-</xsl:text>
<xsl:choose>
<xsl:when test="$mmm = '01'">JAN</xsl:when>
<xsl:when test="$mmm = '02'">FEB</xsl:when>
<xsl:when test="$mmm = '03'">MAR</xsl:when>
<xsl:when test="$mmm = '04'">APR</xsl:when>
<xsl:when test="$mmm = '05'">MAY</xsl:when>
<xsl:when test="$mmm = '06'">JUN</xsl:when>
<xsl:when test="$mmm = '07'">JUL</xsl:when>
<xsl:when test="$mmm = '08'">AUG</xsl:when>
<xsl:when test="$mmm = '09'">SEP</xsl:when>
<xsl:when test="$mmm = '10'">OCT</xsl:when>
<xsl:when test="$mmm = '11'">NOV</xsl:when>
<xsl:when test="$mmm = '12'">DEC</xsl:when>
</xsl:choose>
<xsl:text>-</xsl:text>
<xsl:value-of select="$yyyy" />
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 1808
Reputation: 167446
If you want to change the attribute then you need to adjust the match pattern to match the attribute and of course you then need to create an attribute so you need
<xsl:template match="params/param/queryData/parameter/@value">
<xsl:attribute name="{name()}">
<xsl:call-template name="reformat-date">
<xsl:with-param name="date" select="." />
</xsl:call-template>
</xsl:attribute>
</xsl:template>
instead of
<xsl:template match="params/param/queryData/parameter[@value='*'][normalize-space()]">
<xsl:copy>
<xsl:call-template name="reformat-date">
<xsl:with-param name="date" select="." />
</xsl:call-template>
</xsl:copy>
</xsl:template>
http://xsltransform.net/bEJaofn/1
Upvotes: 4