tags:

views:

34

answers:

1

I have to convert this GMT format to EST in xslt 1.0.

  <Date>Mon, 11 Aug 2009 13:15:10 GMT</Date>

is there any of doing that?

A: 

Here is a pure XSLT solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" >
 <xsl:import href=
  "C:\Program Files\Marrowsoft\Xselerator25\Samples\Libraries\datetime_lib.xsl"/>
 <xsl:output method="text"/>

 <xsl:param name="pnewZone" select="'EST'"/>

 <my:zones>
   <zone name="GMT" offset="+00:00"/>
   <zone name="EST" offset="-05:00"/>
 </my:zones>

 <xsl:template match="/">
   <xsl:variable name="vDay" select=
   "substring-before(substring-after(., ' '), ' ')
   "/>

   <xsl:variable name="vMonthName" select=
   "substring-before(substring-after(., concat($vDay, ' ')), ' ')
   "/>

   <xsl:variable name="vYear" select=
   "substring-before(substring-after(., concat($vMonthName, ' ')), ' ')
   "/>

   <xsl:variable name="vTime" select=
   "substring-before(substring-after(., concat($vYear, ' ')), ' ')
   "/>

   <xsl:variable name="vMonth" select=
   "substring-before(
       substring($default-month-names,
                 string-length(substring-before($default-month-names,
                                                $vMonthName)
                               )-2),
       ']'
                    )
    "/>


  <xsl:variable name="vZone"
   select="substring-after(., concat($vTime, ' '))"/>

  <xsl:variable name="vzoneOffset" select=
   "document('')/*/my:zones/*[@name=$vZone]/@offset"/>

  <xsl:variable name="vnewzoneOffset" select=
   "document('')/*/my:zones/*[@name=$pnewZone]/@offset"/>

  <xsl:variable name="vDateTimeUTC" select=
   "concat($vYear, '-', $vMonth, '-', $vDay, 'T', $vTime, 'Z', $vzoneOffset)"/>

  <xsl:call-template name="local-to-local">
    <xsl:with-param name="datetime" select="$vDateTimeUTC"/>
    <xsl:with-param name="local-offset" select="$vnewzoneOffset"/>
  </xsl:call-template>

 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<Date>Mon, 11 Aug 2009 13:15:10 GMT</Date>

the desired, correct result is produced:

2009-08-11T08:15:10Z-05:00

Do note:

  1. I am using the datetime_lib.xsl template library of Martin Rowlinson. This library is part of the XSelerator, which at present is a free product and can be downloaded at SourceForge.

  2. All processing in the above code is to prepare the parameters to the local-to-local template from the imported library.

  3. I found a bug in local-to-local and corrected it. Replace on line 1143 the following:

    <xsl:when test="($offset != 0) and ($new-offset != 0)">
    

with

    <xsl:when test="($offset != 0) or ($new-offset != 0)">
  1. The transformation of the result to the initial dateTime format is left as exercise to the reader :)
Dimitre Novatchev
Thanks, but for now i am going with C#.Thanks again for all ur time.
Wondering