I have had EXSLT\'s core date functions working well in some XSL templates I\'ve been using for years. I\'d like to start using a new one: seconds
. This function is
For those who want to roll their own (for seconds()
specifically), it can be done rather simply using this code:
package tools;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class Dates
{
/**
* Converts a date in xs:dateTime format into seconds-since-the-epoch
*
* @param date The date to be converted,
* in <code>s:dateTime</code> format (yyyy-MM-dd'T'hh:mm:ss).
*
* @return Number of seconds since <code>1970-01-01 00:00:00</code>,
* or 0 if the date is blank or null.
* @throws ParseException
*/
public static long seconds(String date)
throws ParseException
{
if(null == date || 0 == date.trim().length()) return 0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
Date d = df.parse(date);
return d.getTime() / 1000;
}
}
This can be used through Apache Xalan like this:
<?xml ...
xmlns:java="http://xml.apache.org/xalan/java"
...
<xsl:variable name="some-date" select="...some xs:dateTime data ..." />
<xsl:value-of select="java:tools.Dates.seconds($some-date)" />
I'll probably look at the Xalan source in order to make this more flexible, but for now it seems to be doing what I'd like it to do.
First, have a look at: http://www.exslt.org/howto.html#other-implementations However, AFAIK Xalan does not support the func:script extension element (at least it says it doesn't, which is not always the same). OTOH, Xalan has its own extension mechanism - see: http://xml.apache.org/xalan-j/extensions.html
If it were me, I would simply use a named template instead.