eXSLT to the rescue

XSLT is extremly powerful but limited in many important ways.  For example, it lacks basic date handling functions.  I recently ran into this problem when I was trying to format an RSS feed for my home page.  Luckily, eXSLT solves many of these problems.  Even better, there is a .NET implementation on GotDotNet Workspaces.

I found this RSS xslt stylesheet out on the net (can't remember where) and adapted it to use eXSLT for date formatting. 

Here's how...

First, add the namespace for the functions you want to import:
(such as the date namespace below)

<xsl:stylesheet version="1.0"
   
xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rss="http://purl.org/rss/1.0/"
    xmlns:ns="http://my.netscape.com/rdf/simple/0.9/"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
   
xmlns:date
="http://exslt.org/dates-and-times"
    xmlns="http://purl.org/rss/1.0/">

Next, use a function within your stylesheet.

<xsl:value-of select="date:format-date(./rss:pubDate|./pubDate|./ns:pubDate,'M-dd-yyyy')"/>

Finally, perform the transformation within your Page_Load event.  Since eXSLT has it's own transform object, we can't use the XSL Server Control.  In this example, I just stuff the rendered HTML into the text of a label.

using System.IO;
using
GotDotNet.Exslt;
using
System.Xml.XPath;

private void Page_Load(object sender, System.EventArgs e)
{
    MemoryStream ms=
new
MemoryStream();
    XPathDocument doc =
new
XPathDocument(“http://dotnetweblogs.com/Britchie/Rss.aspx“);
    ExsltTransform xslt =
new ExsltTransform();
    xslt.Load(MapPath("rss.xslt"));
    xslt.Transform(doc,
null
, ms);
    ms.Seek(0,System.IO.SeekOrigin.Begin);
    StreamReader reader=
new
StreamReader(ms);
    XslPlaceHolderLabel.Text=reader.ReadToEnd();
    reader.Clse();
}

That's it!  This opens even more power to the XSLT developer than ever before.  I hope you find eXSLT helpful, I sure did!

 

1 Comment

Comments have been disabled for this content.