Archive for XSLT

RSS date formatting with MSXML4

Damn, I’ve been looking for this for a long time, but I couldn’t find a proper solution. So, what was the problem? Well, I wanted to create a page that gives an overview of several RSS-feeds I follow. The page should replicate some information of these feeds: just the title and the date of the last 5 posts. I wanted to give the date in the feed (i.e. ‘pubDate’) a nice Dutch look and feel, so the rather unreadable “Fri, 15 Sep 2006 13:57:48 +0000” would become “15 september 2006”. These RSS-feeds I parse with an XSLT-stylesheet using (Classic) ASP.

A date in a RSS-feed must be defined as RFC-822. However, I could not use the handy date-time functions for XSLT 2.0 to format this date, because these are not supported by MSXML4, the XML-parser my provider supplies. MSXML supports the ms:format-date function, but this function can handle only standard XSD date formats, which RFC-822 is not. (Bloody hell, I wanted to get back to client-side coding!)

The solution I took is the following: using VBScript in my stylesheet I parse the RFC-822 date as a string and build my Dutch date format with my own hands:


<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:vbs="urn:schemas-sqlxml-org:vbs">

<msxsl:script language="VBScript" implements-prefix="vbs">
  <![CDATA[

   function formateDate(datum)
     dim arr, mnd, x, y, dag, jaar
     arr = Split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", ",")
     mnd = Split("januari,februari,maart,april,mei,juni,juli,augustus,september,oktober,november,december", ",")
     For x = 0 To UBound(arr)
       If InStr(datum, arr(x)) Then
         y = InStr(datum, arr(x))
         dag = Left(datum, y)
         jaar = Mid(datum, y, 8)
         formateDate = stripNr(dag) & " " & mnd(x) & " " & stripNr(jaar)
         exit function
       End If
    Next
    formateDate = ""
   end function

   function stripNr(nr)
     dim arr, str, x
     arr = Split(nr)
     for x = 0 to UBound(arr)
       if isNumeric(arr(x)) then str = str & arr(x)
     next
     stripNr = str
   end function

  ]]>
</msxsl:script>

[... some more code ...]

I’m not sure if this is a good approach, but luckily it works, and it’s good enough for me, because the page I wanted to create is just a start page for my mother with references to several baby blogs: greut.nl/startpagina.

Nonetheless I’m very pleased to get feedback on the code, if you have a better solution, please share it with me!

Tags: , , , , ,

Advertenties

Comments (1)