Thursday, February 22, 2007

Example: using the xml feed

Recently one of our groups asked for more information on how to take an xml feed and turn it into html for their site. What follows is a quick tutorial on what really happens when you "take an xml feed" onto your site, and how to get something running quickly.

Here is a simple process diagram that shows what is happening:




As you can see, the browser request gets to your server, where you need to run some kind of script. For sake of discussion, let us assume you can run Perl as a CGI script on your machine. (The process is the same regardless of if you are using bash, or if you are using PHP, or if you are using ASP.NET.)

This script has two assignments -- first, get the XML from Zanby. Second, transform it into something that you want to send back to the browser.

In Perl, the first part, getting the XML from Zanby, is easy -- there is a wonderful package called LWP::Simple that handles all of the issues of getting information from a remote web server.

The second part, transforming the XML into something to send to the browser, can be as easy or as complex as you want to make it. The easiest approach is to use XSLT to transform your XML into HTML.

So, our complete perl script is pretty simple:


#!/usr/bin/perl

# example of taking Zanby xml feed and wrapping it into html

# Note we are using the "XML::XSLT" package, which also requires
# the XML::DOM, LWP::Simpla, and XML::Parser packages
#
# Assuming you have a relatively clean / modern / complete perl
# installation, you can do "cpan XML::XSLT".

# This is not meant to be complete, good, or otherwise wholesome
# clean-thinking code -- steal at your own risk :-)


use LWP::Simple;
use XML::XSLT;


# get the xml feed

my $xmlstring = get 'http://zanby.com/feed/rrr/groups/?sw=dfl&links&format=4';


# run the xml thru our xsl transform

my $xslfile = "zanby2dfl.xsl";
my $parser = XML::XSLT->new ($xslfile);
my $result = $parser->serve (Source => $xmlstring,
xml_declaration => 0,
http_headers => 0);

# set up appropriate http headers and return

my $length = length ($result);
print "Content-Type: text/html\nContent-Length: $length\n\n$result";




Now, for the XSLT that transforms the Zanby XML into something useful:


<?xml version="1.0" encoding="ISO-8859-1" ?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>DFL Links</h2>
<table border="1">
<tr bgcolor="#7f7fff">
<th align="left">District</th>
<th align="left">Link</th>
</tr>
<xsl:for-each select="groups/MN/group">
<xsl:sort select="name"/>
<tr>
<td><xsl:value-of select="name"/></td>
<td><a>
<xsl:attribute name="href">
<xsl:value-of select="summary"/>
</xsl:attribute>
<xsl:value-of select="name"/>
</a></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>



Here is an example: the DFL Links XML feed turned into HTML using the code above.

Labels:

0 Comments:

Post a Comment

<< Home