Re: [xsl] date formating

Subject: Re: [xsl] date formating
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 17 Aug 2001 14:17:38 +0100
Hi Samina,

> I'm not too sure if this is the answer your looking for, but I'm
> using Xalan and Xerces packages.

Yes, that was the answer I was looking for. You can use Java methods
as extension functions in Xalan. You need to declare a namespace for
the class that you're using. The namespace points to the Java
implementation and assigns a namespace prefix for the functions that
you'll use.

The simplest way of doing so is by declaring the namespace in the
document element of the stylesheet, using the 'xalan' protocol e.g.:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:date="xalan://java.text.SimpleDateFormat"
                extension-element-prefixes="date">
...
</xsl:stylesheet>

You can then call the methods on that class as functions with the
date: prefix. You can create instances of a class using the a new()
function in that namespace. You need to create a SimpleDateFormat
object for the two date formats that you're using:

  <xsl:variable name="prism-date"
                select="date:new('MMM dd yyyy h:mma')" />
  <xsl:variable name="iso-date"
                select='date:new("yyyy-MM-dd&apos;T&apos;hh:mm:ssz")' />

Then you need to parse the harvest_time element using the $prism-date
SimpleDateFormat. You can call an instance method by passing the
instance as the first argument to the function:

  <xsl:variable name="date"
                select="date:parse($prism-date, harvest_time)" />

And format the result of that parse using the $iso-date
SimpleDateFormat:

  <xsl:value-of select="date:format($iso-date, $date)" />

Alternatively, you could write your own class/method in Java that does
the whole conversion, and then call that class/method in the same way
as above.

I haven't tested this, so some of it may be wrong. I've been looking
at the Java API to find out the methods available on
java.text.SimpleDateFormat [1] and the Xalan documentation to work out
how to call methods from there [2]. It's probably worth reading both
yourself.

Cheers,

Jeni

[1] http://java.sun.com/products/jdk/1.2/docs/api/java/text/SimpleDateFormat.html
[2] http://xml.apache.org/xalan-j/extensions.html

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread