using xalan extensions to get the current date and time
TRANSCRIPT
Using xalan extensions to get the current date and time.
We use this area for alerts like server maintenance.
This is an example of an alert message. I want the alert message to no longer show after a given expiration date.
I want to compare the expiration date in <expires/> to the current date.
This is an example of the XML used for the alert message.<display/> defines whether or not to show the message (a manual on/off switch)<expires/> is the date and time after which the message should no longer show.
Source: http://www.w3schools.com/xpath/xpath_functions.asp
XSLT and XPATH Versions 2.0
With XSLT 2.0 and XPATH 2.0, this is how to get the current date and time. WebVoyage uses XSLT 1.0.
header.xsl
1. Define the java.util.GregorianCalendar namespace.2. Import alert.xml and get the expiration date from <expires/>.3. Get the current date.4. Compare the dates.5. Display alert messages based on the result of the comparison.
To get the current date and time, I used xalan extensions.Xalan extensions are installed on your WebVoyage server.
1
23
4
header.xsl
If $display is true and $isExpired is false, display the stylesheet for the alert.
$display is based on the <display/> tag in the imported XML.
$isExpired is true or false depending on whether the current date is greater than or less than the expiration date in the imported XML.
A different stylesheet is used based on the id (headerRow or headerRowAlert)
header.xsl
Again, if $display is true and $isExpired is false, display the alert.
header.xsl
The part where the xalan extensions comes in is in the getCurrentDate template.
header.xsl
We define the ‘cal’ namespace to be a xalan extension using the java.util.GregorianCalendar class from the Java API.
In the template, create a new cal object
java.util.GregorianCalendar[time=1264440457772,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="US/Central",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Central,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=0,WEEK_OF_YEAR=5,WEEK_OF_MONTH=5,DAY_OF_MONTH=25,DAY_OF_YEAR=25,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=27,SECOND=37,MILLISECOND=772,ZONE_OFFSET=-21600000,DST_OFFSET=0]
<xsl:variable name="calendar" select="cal:new()"/><xsl:value-of select="$calendar" />
public static final int YEAR 1public static final int MONTH 2public static final int DAY_OF_MONTH 5public static final int HOUR 11public static final int MINUTE 12public static final int SECOND 13
http://java.sun.com/j2se/1.5.0/docs/api/constant-values.html#java.util.Calendar
If we print the new object, we get a long string of information. We’re interested in the current year, month, day, hour, minute, second. But the constants didn’t work in XSLT.
So I had to look up the constant values.
CONSTANT field values
Defined in java.util.CALENDAR
Using the constant values, I defined variables for the current year, month, day, hour, minute, and second.
The template returns date/time formatted as 2010-01-25 11:27:37
With my variables, I created a string to represent the date. I formatted the string so the template could be re-used.
header.xsl
1. Get the current date using the xalan extension and getCurrentDate template.
2. Get the expiration date from the imported XML.3. Remove punctuation from both variables with translate().4. Turn them into numbers with number().5. Compare the numbers (20100201153112 > 20100125112737).6. The result of the comparison (true/false) is put in the $isExpired
variable.
21
3
45
• The Apache Xalan Project• http://xalan.apache.org/index.html
• Xalan Java Extensions• http://xml.apache.org/xalan-j/extensions.html
• Extensions Library• http://xml.apache.org/xalan-j/extensionslib.html
• Java 2 Platform API• http://java.sun.com/j2se/1.5.0/docs/api/