Re: [xsl] scripting

Subject: Re: [xsl] scripting
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 15 Aug 2001 17:25:24 +0100
Hi Samina,

> I'm converting an xml document to a prism document using a
> stylesheet. One of the elements in the XML document is date, which
> is given in "MM DD YYYY H:MM" format (example: "Jul 10 1999
> 9:10AM"). I want the date to be formated w/ the year first (ex:
> "1999-7-10 9:10AM" ). Can I change my XSL stylesheet to do this? Do
> I need to use bean scripting/java scripting? Is this compatible w/
> JAXP?

As Aleksei said, the most efficient portable method is to write
yourself a template that uses the string manipulation functions
available in XSLT to do a specific conversion between the two date
formats you're dealing with.

If you don't care about portability between processors, then you could
use extension functions to do the conversion or use existing Java
methods. How you use these depends on your processor and what methods
it supports for defining extension functions.

If you care about portability, but don't particularly care about
efficiency and just want to get the job done with the minimal amount
of work, I've just completed a couple of templates for parsing and
formatting dates that you might find useful for a pure XSLT solution.
The stylesheets with them in are at:

  http://www.exslt.org/date/functions/parse-date/date.parse-date.template.xsl
  http://www.exslt.org/date/functions/format-date/date.format-date.template.xsl

To use them, you need to declare the EXSLT - Dates and Times namespace
and import the two stylesheets:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:date="http://exslt.org/dates-and-times";
                extension-element-prefixes="date">

<xsl:import href="date.parse-date.template.xsl" />
<xsl:import href="date.format-date.template.xsl" />

...
                
</xsl:stylesheet>

You can then reformat your dates using something like:

  <xsl:variable name="iso-8601-dateTime">
    <xsl:call-template name="date:parse-date">
      <xsl:with-param name="date-time" select="$date" />
      <xsl:with-param name="format" select="'MMM DD yyyy h:mma'" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:call-template name="date:format-date">
    <xsl:with-param name="date-time" select="$iso-8601-dateTime" />
    <xsl:with-param name="format" select="'yyyy-M-DD h:mma'" />
  </xsl:call-template>

The date format strings use characters in the same way as
java.text.SimpleDateFormat.

Cheers,

Jeni

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


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


Current Thread