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>
This is very straightforward with an
xsl:choose
element, and doesn't require any extensions.This stylesheet
Applied to this XML data
Produces this output
If you can use node-set as extension to your xslt 1.0 processor you can try this.
The output will be:
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.Or you can assign the result to an new variable and use this.
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.