Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
The main gist is that any built-in Java class can be accessed by using the 'xalan://java.package.name.ClassName' syntax. It is easiest if you 'import' the classes you need into your stylesheet using a namespace declaration at the top of the file. For example: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cal="xalan://java.util.GregorianCalendar" xmlns:tz="xalan://java.util.TimeZone" xmlns:fmt="xalan://java.text.SimpleDateFormat"> <xsl:template match="/"> <!-- Stylesheet goodness goes here --> </xsl:template> </xsl:stylesheet> Then later on in your stylesheet, you can call functions on the class using the defined namespace. In this context, static members (aka 'class functions', such as Calendar.getInstance()) are called simply using the namespace and a colon. Other member functions (such as Calendar.set(int field, int value), which operate on a specific instance), take the instance variable as the first parameter, and all other parameters are shifted (so the first parameter becomes the second, the second the third, etc). Here's an example of this which parses a string that contains a GMT time and outputs it as a PST: <xsl:template name="FormatDate"> <xsl:param name="date" /> <!-- incoming format: YYYYMMDDTHHMMSSZ --> <!-- outbound format: MM/DD/YYYY HH:MM AM --> <xsl:variable name="tz_pt" select="tz:getTimeZone('America/Los_Angeles')" /> <xsl:variable name="tz_utc" select="tz:getTimeZone('UTC')" /> <xsl:variable name="fmt_in" select="fmt:new("yyyyMMdd'T'HHmmss")" /> <xsl:variable name="fmt_out" select="fmt:new('MM/dd/yyyy HH:mm:ss a')" /> <!-- Set the output time format to PST, and the input time format to GMT. These statements don't actually output anything. --> <xsl:value-of select="fmt:setTimeZone($fmt_out, $tz_pt)" /> <xsl:value-of select="fmt:setTimeZone($fmt_in, $tz_utc)" /> <xsl:if test="$date != ''"> <xsl:variable name="src_date" select="fmt:parse($fmt_in, $date)" /> <xsl:value-of select="fmt:format($fmt_out, $src_date)" /> </xsl:if> </xsl:template> Note the use of 'xsl:value-of' statements in order to call an instance method on our variables. In XSLT, you have to wrap your method call in a 'select=' type statement, either in a 'xsl:value-of' or as the evaluation of an 'xsl:variable' in order for them to be executed. You could call this somewhere in your style sheet by selecting the date value you wish to convert. <xsl:template match="first_president"> George Washington was born on <xsl:call-template name="FormatDate"> <xsl:with-param name="date" select="birth_date"/> </xsl:call-template> </xsl:template match="first_president">
This paste will be private.
From the Design Piracy series on my blog: