Incorporating XML Content Into Your Web site (ASP)
With the proliferation of RSS feeds, XML formated content is readily available to be incorporated into your website. So how can you do this? Well the first thing you need is XML content to encorporate into your site. Here I am going to demo how to incorporate Jenny Levine’s The Shifted Librarian RSS feed. The url for this feed is http://www.theshiftedlibrarian.com/rss.xml In order to format the Library Stuff content I need to write an XSL file (that’s an XML stylesheet, written in eXtensible Stylsheet Langauge). Below is my XSL file (lstuff.xsl).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"><xsl:output method="html"/>
<xsl:template match="/">
<xsl:element name="h2">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="rss/channel/link"/>
</attribute>
<xsl:value-of select="rss/channel/title"/>
</xsl:element>
</xsl:element><xsl:apply-templates select="rss/channel/item">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="rss/channel/item">
<xsl:element name="h4"><xsl:element name="a"><xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute><xsl:value-of select="title"/></xsl:element>
</xsl:element>
<xsl:element name="p">
<xsl:value-of select="description" disable-output-escaping="yes"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Once your have constructed your XSL file then you are ready to put these two files together. In my case I use ASP and the MSXML parser in order to display the XML as part of my webpage. However, you can use PHP to this as well. Below is my sample code.
<%
Function getXML(sourceFile)
dim styleFile
dim source, style
styleFile = Server.MapPath("lstuff.xsl")
Dim xmlhttp
Set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", sourceFile, false
xmlhttp.Send
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.loadxml(xmlhttp.ResponseText)
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)
getXML = source.transformNode(style)
set source = nothing
set style = nothing
End Function
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Demo</title>
<meta content="text/html; charset=windows-1252" http-equiv=content-type>
<link rel="stylesheet" type="text/css" href="wc_main.css">
</head>
<body>
<%= getXML("http://www.theshiftedlibrarian.com/rss.xml")%>
</body>
</html>
The really neat thing about this is that as the XML source changes so does the page and you don’t have to do anything in order for it to be updated. You can also use this code along with a web-based form and a database to create your own news aggregator. More on this in another post.