Translating RSS With XSLT
Introduction
RSS, Real Simple Syndication, is an XML dialect used by bloggers to hold their
channels and entries. Other sites use RSS to describe the content on a page
or recent articles they have written. XSLT is a language used to transform
XML documents into other documents. In this article we will be transforming RSS
into HTML. I will demonstrate how pulling and transforming information describing
my articles.
To transform the XSLT we will need the System.Xml and System.Xml.Xsl namespaces.
The three main classes we will use are XmlDocument, XslTransform, and XmlReader The process
to transform the XML to HTML is actually very simple. For the following example to work we will be
creating the files "Article.xml", "RssArticles.xslt", and "RssArticles.aspx" Let's get started.
The RSS Feed
The example below is the RSS feed designed to describe the articles I have on this site.
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>Jeff Julian's Articles</title>
<link>http://www.jjulian.com/Articles.aspx</link>
<description>
These are articles Jeff Julian has published on http://www.dotnetjunkies.com.
</description>
<language>en-us</language>
<lastBuildDate>Sat, 17 May 2003 22:29:39 GMT</lastBuildDate>
<docs>http://backend.userland.com/rss2</docs>
<generator>Jjulian.com RssMaster 1.1</generator>
<category>Articles</category>
<item>
<title>ToString() or Not ToString()?</title>
<link>http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=710</link>
<description>
ToString() or not ToString()? That is the question. One of the many misconceptions
of the .NET framework occurs when a developer adds characters to the end of a string
after the string is created, and expects the same concatenation occured in memory.
In reality, it doesn't happen this way.
</description>
<pubDate>2/5/2003</pubDate>
<author>jjulian2@mac.com</author>
</item>
<item>
<title>Check Your Email With Style</title>
<link>http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=753</link>
<description>
Have you ever wanted to write your own POP3 email client? Well now you
can with the help of my C# example. This article explains how to use
.NET to connect to a POP3 mail server, and the different commands to
use with the server to get the results you want.
</description>
<pubDate>3/11/2003</pubDate>
<author>jjulian2@mac.com</author>
</item>
<item>
<title>How To View Graphics Online With WinForms</title>
<link>http://www.dotnetjunkies.com/howto/default.aspx?id=58</link>
<description>
Have you ever needed an image online for a form but you don't want to save it on
the local machine. In this example, I will show you a simple way of getting this
task done, all by the push of a button.
</description>
<pubDate>3/27/2003</pubDate>
<author>jjulian2@mac.com</author>
</item>
</channel>
</rss> |
The XSLT Document
Next we will need the XSLT document to translate our RSS feed.
The example below will transform the article feed into HTML with only the elements we want to display.
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="rss/channel">
<html>
<head>
<title><xsl:value-of select="title" /></title>
<style media="all" lang="en" type="text/css">
.ChannelTitle
{
font-family: Verdana;
font-size: 11pt;
font-weight: bold;
width: 500px;
text-align: center;
}
.ArticleEntry
{
border-width: 2px;
border-color: #336699;
border-style: solid;
width: 500px;
}
.ArticleTitle
{
background-color: #3366CC;
color: #FFFFFF;
font-family: Verdana;
font-size: 9pt;
font-weight: bold;
padding-left: 5px;
padding-top: 5px;
padding-bottom: 5px;
}
.ArticleHeader
{
background-color: #3399FF;
color: #FFFFFF;
font-family: Verdana;
font-size: 7pt;
padding-left: 5px;
padding-top: 2px;
padding-bottom: 2px;
}
.ArticleHeader A:visited
{
color: #FFFFFF;
text-decoration: none;
}
.ArticleHeader A:link
{
color: #FFFFFF;
text-decoration: none;
}
.ArticleHeader A:hover
{
color: #FFFF00;
text-decoration: underline;
}
.ArticleDescription
{
color: #000000;
font-family: Verdana;
font-size: 9pt;
padding-left: 5px;
padding-top: 5px;
padding-bottom: 5px;
padding-right: 5px;
}
</style>
</head>
<body>
<xsl:apply-templates select="title" />
<xsl:apply-templates select="item" />
</body>
</html>
</xsl:template>
<xsl:template match="title">
<div class="ChannelTitle">
<xsl:value-of select="text()" />
</div>
<br />
</xsl:template>
<xsl:template match="item">
<div class="ArticleEntry">
<div class="ArticleTitle">
<xsl:value-of select="title" />
</div>
<div class="ArticleHeader">
<a href="{link}">Link</a> - <xsl:value-of select="pubDate" /> - <a href="mailto:{author}">Email The Author</a>
</div>
<div class="ArticleDescription">
<b>Description:</b> <xsl:value-of select="description" />
</div>
</div>
<br />
</xsl:template>
</xsl:stylesheet> |
The Transform WebForm
The next thing we need to do is create a new WebForm and name it "RssArticles.aspx".
Once this is done, we need to go into the .aspx file and delete all of the HTML the IDE created for us.
We do this so our XSLT can render the proper HTML tags. Now either switch to the code behind or build the server-side scripting block. Go to or create a Page_Load method.
Now create the following code for the transform using the classes we discussed above.
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument _xmlDocument;
XmlDocument _xmlOutput;
XmlReader _xmlReader;
XslTransform _xslTransform;
_xmlDocument = new XmlDocument();
_xmlOutput = new XmlDocument();
_xslTransform = new XslTransform();
_xmlDocument.Load(Server.MapPath("Articles.xml"));
_xslTransform.Load(Server.MapPath("RssArticles.xslt"));
_xmlReader = _xslTransform.Transform(_xmlDocument, new XsltArgumentList());
_xmlOutput.Load(_xmlReader);
Response.Write(_xmlOutput.OuterXml);
}
|
The Output
After the code is finished, build the project and use a browser to navigate to the page.
This is what the HTML output should look like.
Jeff Julian's Articles
ToString() or Not ToString()?
Description:
ToString() or not ToString()? That is the question. One of the many misconceptions
of the .NET framework occurs when a developer adds characters to the end of a string
after the string is created, and expects the same concatenation occured in memory.
In reality, it doesn't happen this way.
Check Your Email With Style
Description:
Have you ever wanted to write your own POP3 email client? Well now you
can with the help of my C# example. This article explains how to use
.NET to connect to a POP3 mail server, and the different commands to
use with the server to get the results you want.
How To View Graphics Online With WinForms
Description:
Have you ever needed an image online for a form but you don't want to save it on
the local machine. In this example, I will show you a simple way of getting this
task done, all by the push of a button.
Conclusion
To summarize this article, we used three technologies to build this transformer: RSS, XSLT, and .NET.
The process was relatively painless and can be reused to view different RSS feeds. RSS is becoming
one of the most used technologies in the industry. Developers are wrapping their sites, blogs, forums, and other web items into RSS feeds to allow other to use the content how they see fit.
I hope this article was interesting and informative. If you have any questions, please feel free to email me at jjulian2@mac.com and I will try to assist you.
To use my current blog RSS feed, go to http://jjulian.geekswithblogs.com/Rss.aspx. To view my blog, go to http://jjulian.geekswithblogs.com/Blog.aspx.