Kapil
Kapil

Reputation: 832

Date time in xslt

Hi i am having the xml file as:

<order><Extension Properties><Date>2009-08-04T17:09:04.593+05:30</Date></Extension  Properties></Order>

and i want the output as

Generation Date 040809

I want to do this via xslt.Please help..!!

Upvotes: 0

Views: 2763

Answers (3)

MyItchyChin
MyItchyChin

Reputation: 14031

Using this as a guide...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <order>
      <xsl:element name="GenerationDate">
        <xsl:call-template name="FormatDate">
          <xsl:with-param name="DateTime" select="order/Extension/Date"/>
        </xsl:call-template>
      </xsl:element>
    </order>
  </xsl:template>
 
  <xsl:template name="FormatDate">
    <xsl:param name="DateTime" />
    <xsl:variable name="day">
      <xsl:value-of select="substring($DateTime,9,2)" />
    </xsl:variable>
    <xsl:variable name="month">
      <xsl:value-of select="substring($DateTime,6,2)" />
    </xsl:variable>
    <xsl:variable name="year">
      <xsl:value-of select="substring($DateTime,3,2)" />
    </xsl:variable>
    <xsl:value-of select="$day"/>
    <xsl:value-of select="$month"/>
    <xsl:value-of select="$year"/>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Chris Nielsen
Chris Nielsen

Reputation: 14748

One way is to use the substring functions:

<xsl:variable name="d" select="/order/Extension/Date" />
Generation Date <xsl:value-of select="concat(
substring($d, 9, 2),
substring($d, 6, 2),
substring($d, 3, 2))"/>

Other answers might depend on the XSL engine you are using. For instance, if you are using MSXML, you can use the datetime extension functions:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms="urn:schemas-microsoft-com:xslt">
    <xsl:output method="xml" />

    <xsl:template match="/">

        <xsl:template match="Date">
        <xsl:variable name="d" select="/order/Extension/Date" />
        Generation Date
        <xsl:value-of select="ms:format-date($d, 'ddMMyy')"/>

    </xsl:template>

</xsl:stylesheet>

Good luck!

Upvotes: 1

John McG
John McG

Reputation: 698

<xsl:variable name="dateString" select="order/Extension/Date/> (or something along those lines)

<xsl:value-of select="substring($date,9,10)"><xsl:value-of select="substring($date,6,7)"><xsl:value-of select="substring($date,1,4)">

Upvotes: 0

Related Questions