Introduction
Creating an XML newsfeed is a great way to get to grips with a real world use of XML. This tutorial, taken from CodeToad.Com will explain how to deal with the XML feeds offered by various sites on the web at the moment, and turn them into a useable web page. We will use a style sheet and a little ASP to do the job.
Click here to see how the final newsfeed page will look.
A full list of XML newsfeeds from moreover.com is available at http://w.moreover.com/categories/category_list_xml.html%20target=_blank. Amazon affiliates are also offered the chance to create their links in XML which increases the power of searches, and product placements.
We're going to use the xmlHTTP
object within an ASP page to grab the XML from the URL provided by the XML-generating web site. For more information on xmlHttp
see my XML HTTP Tutorial.
Once we have the XML, we call the style sheet to display the page.
Here's how the ASP page looks then :
<%
dim objHTTP , objXML , objXSL
set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objHTTP.open "GET",
"http://p.moreover.com/cgi-local/page?c=Pop%20music%20reviews&o=xml",
false
objHTTP.send
set objXML = objHTTP.responseXML
set objXSL=Server.CreateObject("microsoft.xmldom")
objXSL.async=false
objXSL.load(Server.MapPath("style.xsl"))
if (objXSL.parseError.errorCode = 0) then
Response.Write(objXML.transformnode(objXSL))
else
Response.Write "Error: " & objXSL.parseError.reason
& "<br> URL:" & objXSL.url
end if
Set objHTTP = Nothing
Set objXML = Nothing
Set objXSL = Nothing
%>
We've referenced a style sheet called style.xsl. Our XSL style sheet loops through each item in the XML and displays the record. Obviously, you'll need to look at the XML that you have and adapt the style sheet as necessary, but this should be enough to give you an idea:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<TITLE>moreover...</TITLE>
</head>
<body BGCOLOR="ffffff">
<br/><br/>
<br/> br
<DIV ALIGN="center">
<TABLE BGCOLOR="ffffff" BORDER="0" CELLPADDING="4"
CELLSPACING="0" WIDTH="100%">
<xsl:for-each select="moreovernews/article">
<TR VALIGN="middle">
<TD ALIGN="left" BGCOLOR="ffffff">
<B>
<A>
<xsl:attribute name="HREF">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<FONT FACE="Verdana, Arial,Helvetica,
sans-serif" SIZE="-1" COLOR="000000">
<xsl:value-of select="headline_text"/>
</FONT>
</A>
</B>
<BR/>
<A>
<xsl:attribute name="HREF">
<xsl:value-of select="document_url"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<FONT FACE="Verdana, Arial,Helvetica,
sans-serif" SIZE="-2" COLOR="666666">
<xsl:value-of select="source"/>
</FONT>
</A>
<A>
<xsl:attribute name="HREF">
<xsl:value-of select="access_registration"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<FONT FACE="Verdana, Arial,Helvetica,
sans-serif" SIZE="-2" COLOR="666666">
<xsl:value-of select="access_status"/>
</FONT>
</A>
<FONT FACE="Verdana, Arial,Helvetica,
sans-serif" SIZE="-2" COLOR="666666">
<xsl:value-of select="harvest_time"/> GMT
</FONT>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</DIV>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here's what the resulting page looks like. Remember we point to the original ASP page to do the work and pull in both the XML and the Style Sheet. Our newsfeed pulled in pop music news, so Click here for the latest popular music news.