XSLT convert date format

biztalk, xml, xpath, xslt, xslt-1.0

Solution

Your question is a little vague. It needs some sample input and expected output. But any way, here is an answer to best guess at what you want.

Given this input:

<?xml version="1.0"?>
<dates>
  <date>11/12/2012</date>
  <date>3/4/2011</date>
</dates>

... transformed by this style-sheet ...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

      <xsl:template match="dates">
        <xsl:copy>
        <xsl:apply-templates select="*" />
        </xsl:copy>
      </xsl:template>

      <xsl:template match="date">
        <xsl:copy>
        <xsl:value-of select=" 
           concat(
           substring-after(substring-after(.,'/'),'/') , '-',
           format-number( number( substring-before(.,'/')), '00') , '-',
           format-number( substring-before(substring-after(.,'/'),'/'), '00') 
           )
          " />
        </xsl:copy>
      </xsl:template>

</xsl:stylesheet>

... will produce this desired output ...

<dates>
 <date>2012-11-12</date>
 <date>2011-03-04</date>
</dates>

Please tick the answer if it is correct. I verified this solution on http://www.purplegene.com/static/transform.html

Problem

I'm having an issue where I'm getting an XML file, and from the samples that I get the format of date that I'm getting is `mm/dd/yyyy` and sometimes it is `m/d/yyyy`. My task is to convert this to another XML file where the schema only accepts `yyyy-mm-dd`. I'm limited to using XSLT 1.0/XPATH 1.0. How can I do this?

Original source

Related problems