Aman Chauhan
Aman Chauhan

Reputation: 95

Convert date format in xslt from YYYYMMDD to MM/DD/YYYY

I am Having date in xml file in format like

YYYYMMDD

Applying the xslt transformation i want to change the format to

MM/DD/YYYY

.

For example, Incoming format - 20160513 Output format - 05/13/2016

Upvotes: 1

Views: 5016

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52878

XSLT 2.0 option...

<xsl:template match="date[matches(normalize-space(),'^\d{8}$')]">
  <xsl:copy>
    <xsl:value-of select="replace(normalize-space(),
      '(\d{4})(\d{2})(\d{2})',
      '$2/$3/$1')"/>
  </xsl:copy>
</xsl:template>

Upvotes: 3

michael.hor257k
michael.hor257k

Reputation: 117073

Given:

<date>20160513</date>

the following:

<xsl:template match="date">
    <xsl:copy>
        <xsl:value-of select="substring(., 5, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 7, 2)"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(., 1, 4)"/>
    </xsl:copy>
</xsl:template>

will return:

<date>05/13/2016</date>

Upvotes: 3

Related Questions