Archive

Archive for March, 2011

Date parsing and formatting in XSL

One task I’ve found to be the bane of my existence when dealing with XSL is date formatting.  There’s no a good way to do it, and it makes things extremely difficult always needing to write new XSL because no 2 dates ever have the same format.

Since I’ve written more than my share of date parsing templates, I decided that I would try to do it once more, and this would be the last time I’d ever write one.  The DateFormatting.xsl contains callable templates for most common operations.

Format Strings

The majority of the templates require a format string for input and/or output.  The syntax for the string is as follows:

m – the numeric value for the month i.e. 8, 9, or 10

mm – the 2 digit padded numeric value for the month i.e. 08, 09, or 10

mmm – the short month name i.e. Aug, Sept, or Oct

mmmm – the long month name i.e. August, September, or October

d – the numeric value for the date i.e. 89, or 10

dd – the 2 digit padded numeric value for the date i.e. 0809, or 10

yy – the 2 digit century i.e. 0809, or 10

yyy – the 4 digit century i.e. 2008, 2009, or 2010

ww – the short day of the week i.e. Sun, Mon, or Tue

www – the full name for the day of the week i.e. Sunday, Monday, or Tuesday

Any other characters are considered delimiters and are not evaluated as part of the date.

So the date 01-Jan-2011 would use the format “dd-mmm-yyy”

Templates

The templates are callable templates.  There are internal templates that begin with an underscore.  The main templates (with no underscore) are mostly wrappers around the internal templates and provide the ability to call the templates with strings instead of structured xml dates – I’ll explain the main ones below:

FormatDate – takes in a date string and returns a new date string in the specified format.  It accepts 3 parameters: inputFormatString, outputFormatString, & dateTimeString.  The inputFormatString specifies the format of the dateTimeString parameter.  The outputFormatString specifies the desired format of the returned date.  The dateTimeString contains the actual date to be formatted (pretty obvious I would think, but you never know).

Example:

.

.

.

Result:   20100130

ParseDate – parses a date and returns a new XML structure containing the raw date values.  Input params are the format string and the date string.

Ex:

.

.

.

Result:

.

.

.

CalculateDayOfWeek – parses a date and returns the numeric day of the week. Sunday = 1, Monday = 2, etc…

Ex:

.

.

.

Result:  4

** The rest of these follow a similar format and return a number or formatted string so I will not include the examples or results for the sake of brevity…

CalculateTotalDaysSinceEpoch – parses a date and returns the number of days since January 1, 0000.

CalculateTotalSecondsSinceEpoch – this method is somewhat pointless since the script does not take into account time – only dates.  So right now this just takes the DaysSinceEpoch and multiplies it by the seconds in a day.

MonthShortString – returns the short name of the provided month.  i.e. “Jan”, “Feb”…

MonthLongString – returns the full name of the provided month. i.e. “January”, “February”…

MonthFromShortName – returns the month number from the provided short name.

MonthFromLongName – returns the month number from the provided long name.

WeekDayLongName – returns the full name of the provided weekday number.

WeekDayShortName – returns the short name of the provided weekday number.

WeekDayFromLongName – returns the day number from the full weekday name.

WeekDayFromShortName – returns the day number from the short weekday name.

You can find the full XSL code along with a test stylesheet and some sample data here.

Categories: Uncategorized