JimRobinson_Xalan_Extensions_Chicago

Download Report

Transcript JimRobinson_Xalan_Extensions_Chicago

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.
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.
I want to compare the expiration date in <expires/> to the current date.
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.
Source: http://www.w3schools.com/xpath/xpath_functions.asp
To get the current date and time, I used xalan extensions.
Xalan extensions are installed on your WebVoyage server.
header.xsl
1
2
3
4
1.
2.
3.
4.
5.
Define the java.util.GregorianCalendar namespace.
Import alert.xml and get the expiration date from <expires/>.
Get the current date.
Compare the dates.
Display alert messages based on the result of the comparison.
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
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.
<xsl:variable name="calendar" select="cal:new()"/>
<xsl:value-of select="$calendar" />
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]
public static final int YEAR
public static final int MONTH
public static final int DAY_OF_MONTH
public static final int HOUR
public static final int MINUTE
public static final int SECOND
1
2
5
11
12
13
So I had to look up the constant values.
http://java.sun.com/j2se/1.5.0/docs/api/constant-values.html#java.util.Calendar
Using the constant
values, I defined
variables for the
current year, month,
day, hour, minute, and
second.
With my variables, I
created a string to
represent the date. I
formatted the string so
the template could be
re-used.
The template returns date/time formatted as 2010-01-25 11:27:37
header.xsl
1
2
5
1.
2.
3.
4.
5.
6.
3
4
Get the current date using the xalan extension and getCurrentDate template.
Get the expiration date from the imported XML.
Remove punctuation from both variables with translate().
Turn them into numbers with number().
Compare the numbers (20100201153112 > 20100125112737).
The result of the comparison (true/false) is put in the $isExpired variable.
• 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/