damien535
damien535

Reputation: 637

Date and time conversions

My problem is that I receive for example a date in a concatenated format:

Ex: 20050728

And I have to retrieve it in a readable format through my xslt.

Ex. 28 July 2005

I also have a similar question regards time.

Ex: 0004

To be displayed as 00:04

How is this done?

Upvotes: 2

Views: 2051

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

Use the XPath substring() function as shown in the solution below:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <!--                                           -->
 <xsl:variable name="vrMonths">
   <m>January</m>
   <m>February</m>
   <m>March</m>
   <m>April</m>
   <m>May</m>
   <m>June</m>
   <m>July</m>
   <m>August</m>
   <m>September</m>
   <m>October</m>
   <m>November</m>
   <m>December</m>
 </xsl:variable>
 <!--                                           -->
 <xsl:variable name="vMonths" select=
  "document('')/*/xsl:variable[@name='vrMonths']/*"/>
<!--                                           -->
    <xsl:template match="date">
      <xsl:value-of select=
       "concat(substring(.,7), ' ',
             $vMonths[number(substring(current(),5,2))], ' ',
             substring(.,1,4))"
       />
    </xsl:template>
 <!--                                           -->
    <xsl:template match="time">
     <xsl:value-of select=
     "concat(substring(.,1,2),':',substring(.,3))"/>
    </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on this XML document:

<t>
 <date>20050728</date>
 <time>0004</time>
</t>

the wanted result is produced:

 28 July 2005
 00:04

Upvotes: 4

annakata
annakata

Reputation: 75824

xslt has some formatting functions available

Upvotes: 2

Hank Gay
Hank Gay

Reputation: 71949

If you can use XPath 2.0, see this function reference.

Upvotes: 3

Related Questions