AlbionKyle
AlbionKyle

Reputation: 53

XSLT convert SimpleDateTime to GMT-00:00 timezone

I need help on XSLT to reformat time, without much luck.

<names> 
    <name>
        <foo id='x_date'>
             <value> 01/23/2011 13:20:00 PDT</value> 
        </foo> 
    </name>
</names>

How will I change the date from '01/23/2011 01:23:00 PDT' to '01/23/2011 09:24:00 GMT+00:00' via XSLT?

Please help, it's killing me :-)

Upvotes: 1

Views: 846

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220922

If you're open to use a non-XSLT solution using Java's Xalan extensions, for instance, you could opt for the date time functions as documented here:

http://exslt.org/date/index.html

Something along the lines of

<xsl:value-of select="
  date:format-date(
    date:parse-date(/names/name/foo/value, $inPattern),
    $outPattern)" />

In your specific case, you'd probably have to implement your own date formatter in a custom namespace. This is quite simple:

  • Add Xalan to your classpath
  • Create a custom date formatter:
    package org.example;
    public class MyExtension {
      public static String myFormat(String date) {
        // Do the formatting
      }
    }
  • Use the above formatter in an XSLT stylesheet:
    <xsl:stylesheet xmlns:myextension="http://org.example.MyExtension">
      ..
      <xsl:value-of select="myextension:myFormat(/names/name/foo/value)"/>
      ..
    </xsl:stylesheet>

More documentation can be found here:

http://exslt.org

Upvotes: 1

Related Questions