Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL Date formating

Status
Not open for further replies.

danmat46

Programmer
Nov 13, 2007
23
GB
This is probably the worse thing I have ever attempted in xsl

formating a date value that can be any of the following:

MM/DD/YY
M/DD/YY
M/D/YY
MM/D/YY

changing it into YY-MM-DD

Any guidance?
 
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="xml"/>

   <xsl:template match="date">
       <xsl:variable name="month" select="substring-before(.,'/')" />
       <xsl:variable name="MM">
       <xsl:choose>
       <xsl:when test='string-length($month) = 1'>-0</xsl:when>
       <xsl:when test='string-length($month) = 2'>-</xsl:when>
       </xsl:choose>
       </xsl:variable>

       <xsl:variable name="stem" select= "substring-after(.,concat($month,'/'))" />
       <xsl:variable name="day" select="substring-before($stem,'/')" />
       <xsl:variable name="DD">
       <xsl:choose>
       <xsl:when test='string-length($day) = 1'>-0</xsl:when>
       <xsl:when test='string-length($day) = 2'>-</xsl:when>
       </xsl:choose>
       </xsl:variable>

       <xsl:variable name="year" select="substring-after($stem,'/')" />

       <xsl:variable name="formatted_date" select="concat($year,$MM,$month,$DD,$day)" />

       <xsl:value-of select="concat(.,'   ',$formatted_date)"/>
   </xsl:template>

</xsl:stylesheet>
For the following sample file
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- change to YY-MM-DD -->
<Dates>
   <date>12/31/08</date>
   <date>1/31/08</date>
   <date>1/1/08</date>
   <date>12/1/08</date>
</Dates>
the output is
Code:
   12/31/08   08-12-31
   1/31/08   08-01-31
   1/1/08   08-01-01
   12/1/08   08-12-01
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top