| |
Using XML data from the Washington
Post
Back to Geek Stuff Key components:
...for the feeds from the Washington Post (like this Washington
Redskins feed), I use this stylesheet, PostRSSFull.xsl. This yields
the page headlines.asp.
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <xsl:apply-templates select="//channel"/> </xsl:template> <xsl:template match="channel"> <xsl:element name="p"> <xsl:element name="a"> <xsl:attribute name="href"><xsl:value-of select="./link"/></xsl:attribute> <xsl:attribute name="class">medium</xsl:attribute> <xsl:value-of select="./title"/> </xsl:element> </xsl:element> <xsl:element name="table"> <xsl:for-each select="./item"> <xsl:apply-templates select="."/> </xsl:for-each> </xsl:element> </xsl:template> <xsl:template match="item"> <xsl:element name="tr"> <xsl:element name="td"> <xsl:attribute name="valign">top</xsl:attribute> <xsl:attribute name="class">c5</xsl:attribute> <xsl:element name="a"> <xsl:attribute name="href"><xsl:value-of select="./link"/></xsl:attribute> <xsl:value-of select="title"/> </xsl:element> <xsl:text> </xsl:text> <xsl:if test="author!= ''"> <xsl:value-of select="author"/> <xsl:text>, </xsl:text> </xsl:if> <xsl:value-of select="substring(pubDate, 1, 11)"/> </xsl:element> </xsl:element> <xsl:element name="tr"> <xsl:element name="td"> <xsl:attribute name="valign">top</xsl:attribute> <xsl:attribute name="class">c5</xsl:attribute> <xsl:value-of disable-output-escaping="yes" select="description"/> </xsl:element> </xsl:element> <xsl:element name="tr"> <xsl:element name="td"> <xsl:text disable-output-escaping="yes"> </xsl:text> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet>
|
When writing these pages, I simplified the calls and functions to perform
the transformations of the data. First, I created an include file called
xmlFunctions.asp, which contains the following:
<% ' xmlFunctions.asp ' Contains basic functions to take advantage of XML feeds throughout the web. '
Dim SRC_LOCAL
Dim SRC_WEB
SRC_LOCAL = 0
SRC_WEB = 1
Function createXmlDomDocument(xd)
On Error Resume Next
'Uncomment the following line to use MSXML 3.0 Set xd = CreateObject("MSXML2.DOMDocument.3.0") 'Uncomment the following line to use MSXML 4.0 'Set xd = CreateObject("MSXML2.DOMDocument.4.0") 'Uncomment the following line to use MSXML 5.0
'Set xd = CreateObject("MSXML2.DOMDocument.5.0")
If (IsObject(xd) = False) Then
alert("DOM document not created. Check MSXML version used in createXmlDomDocument.")
Else
Set createXmlDomDocument = xd
End If
End Function
function transformAndDisplay(xmlData, srcType, xmlStyleSheet)
' pass in file names, returns a string containing the transformed information (in HTML format) ' also pass in srcType to see if xmlData source is local or somewhere out on the web, because
' we will have to treat it slightly differently
dim xmlDoc
dim xmlDocPath
dim xslDoc
set xmlDoc = createXmlDomDocument(xmlDoc)
set xslDoc = createXmlDomDocument(xslDoc)
xmlDoc.validateOnParse = false
xmlDoc.async = false
if srcType = SRC_LOCAL then
xmlDocPath = server.MapPath(xmlData)
else
' srcType = SRC_WEB
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDocPath = xmlData
end if
xmlDoc.load xmlDocPath
//response.write("Input document: " & xmlDocPath)
xslDoc.validateOnParse = False
xslDoc.load server.MapPath(xmlStyleSheet)
'response.write(server.MapPath(xmlStyleSheet))
'response.write(xslDoc.xml)
response.write(xmlDoc.transformNode(xslDoc))
end function
function transformAndDisplayWithParameters(xmlData, srcType, xmlStyleSheet, sParmNames, sParmValues)
'on error resume next
dim xslt
dim xslDoc
dim xmlDoc
dim xslProc
dim arrParms
dim arrParmValues
set xslt = createObject("Msxml2.XSLTemplate.3.0")
set xslDoc = createObject("Msxml2.FreeThreadedDOMDocument.3.0")
set xmlDoc = createObject("Msxml2.DOMDocument.3.0")
'set xslProc = xslt.createProcessor()
xslDoc.async = False
xslDoc.Load server.MapPath(xmlStyleSheet)
If (xslDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xslDoc.parseError
response.write("You have error " & myErr.reason)
response.end
Else
Set xslt.stylesheet = xslDoc
xmlDoc.async = False
if srcType = SRC_LOCAL then
xmlDocPath = server.MapPath(xmlData)
else
' srcType = SRC_WEB
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDocPath = xmlData
end if
xmlDoc.load xmlDocPath
End If
arrParms = split(sParmNames, "|")
arrParmValues = split(sParmValues, "|")
If (xmlDoc.parseError.errorCode <> 0) Then
Set myErr = xmlDoc.parseError
'MsgBox("You have error " & myErr.reason)
response.write(xmlDoc.parseError)
Else
Set xslProc = xslt.createProcessor
xslProc.input = xmlDoc
i = 0
for i = 0 to UBound(arrParms)
xslproc.addParameter arrParms(i), arrParmValues(i)
next
xslProc.Transform
'MsgBox xslProc.output
End If
response.write(xslProc.output)
end function %>
|
Now I can make a simple call when I want to transform anything -- see
the code and HTML of headline.asp with
the function call in red below.
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--#include file="../include/xmlfunctions.asp" --> <% dim headlineType dim feeds(2) feeds(0) = "http://www.washingtonpost.com/wp-srv/topnews/rssheadlines.xml" feeds(1) = "http://www.washingtonpost.com/wp-srv/sports/leaguesandsports/nfl/20002001" & _
"/washingtonredskins/rssheadlines.xml" feeds(2) = "http://www.washingtonpost.com/wp-srv/sports/colleges/20002001" & _
"/universityofmaryland/rssheadlines.xml"
dim types(3)
types(0) = "Headlines from the Washington Post" types(1) = "Washington Redskins Headlines from the Washington Post" types(2) = "University of Maryland Sports from the Washington Post"
headlineType = request.querystring("pHL")
if headlineType = "" then
headlineType = 0
end if
%>
<title><%=types(headlineType)%></title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<link href="../include/styles2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p><a href='javascript:self.close();'>Close this window</a></p>
<%=transformAndDisplay(feeds(headlineType), SRC_WEB, "../xml/PostRSSFull.xsl")%>
<p><a href='javascript:self.close();'>Close this window</a></p>
</body>
</html>
|
If you want to use any of this code, you're welcome to do so. All I
ask is that you credit me and this site (Brandt Barretto at http://www.barretto.org/).
We appreciate your support!
|