using xalan extensions to get the current date and time

16
Using xalan extensions to get the current date and time.

Upload: paula-gardner

Post on 30-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using xalan extensions to get the current date and time

Using xalan extensions to get the current date and time.

Page 2: Using xalan extensions to get the current date and time

We use this area for alerts like server maintenance.

Page 3: Using xalan extensions to get the current date and time

This is an example of an alert message. I want the alert message to no longer show after a given expiration date.

Page 4: Using xalan extensions to get the current date and time

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.

Page 5: Using xalan extensions to get the current date and time

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.

Page 6: Using xalan extensions to get the current date and time

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

Page 7: Using xalan extensions to get the current date and time

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)

Page 8: Using xalan extensions to get the current date and time

header.xsl

Again, if $display is true and $isExpired is false, display the alert.

Page 9: Using xalan extensions to get the current date and time

header.xsl

The part where the xalan extensions comes in is in the getCurrentDate template.

Page 10: Using xalan extensions to get the current date and time

header.xsl

We define the ‘cal’ namespace to be a xalan extension using the java.util.GregorianCalendar class from the Java API.

Page 11: Using xalan extensions to get the current date and time

In the template, create a new cal object

Page 12: Using xalan extensions to get the current date and time

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.

Page 13: Using xalan extensions to get the current date and time

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.

Page 14: Using xalan extensions to get the current date and time

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.

Page 15: Using xalan extensions to get the current date and time

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

Page 16: Using xalan extensions to get the current date and time

• 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/