How to turn an XML feed into something useful
March 13th, 2007 by mikeBefore we begin, a warning: this post is fairly technical, so if you’re looking for some light wine-related reading I suggest you skip this article. I’m assuming you already know at least the basics of working with XML and PHP (though not necessarily using them together). If you’ve never heard of these technologies, then what follows will likely be completely meaningless to you.
If you’re used to working with XML, but have never bothered to learn about XSL/XSLT, you’re missing out. XSLT, or eXtensible Stylesheet Language Transformations, is an XML language that can transform an XML document into something else… What else? Well, theoretically, anything that can be represented as ones and zeros. We’ll be using it to transform Vino2Vino’s XML wine feeds into a variety of useful formats.
To begin, we need an XML source document. I’ll be working with my personal wine list from Vino2Vino.com, which is located at http://vino2vino.com/feed/wine/mike, and looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<wines count="9">
<link>
http://www.vino2vino.com/feed/wine/mike
</link>
<wine id="38681">
<color>unknown</color>
<name>Amarone della Valpolicella
Classico Campolongo di Torbe</name>
<year>1999</year>
<production-volume>1600</production-volume>
<winery id="2436">Masi</winery>
<region id="57" country="IT" continent="EU">
Veneto
</region>
<price>85</price>
<ratings>
<rating by="V2V">85</rating>
<rating by="WS">90</rating>
</ratings>
<tags>
<tag>silky</tag>
<tag>rich</tag>
<tag>full bodied</tag>
<tag>full</tag>
<tag>plums</tag>
</tags>
</wine>
...
</wines>
If you’re working with a different markup language you’ll need to adjust the examples accordingly.
Let’s convert our XML feed into a format that a normal feed reader can understand, that way I can subscribe to my wine feed and have it show up on my Google homepage, in the sidebar of my blog, etc. All I really want are the names of the wines with links to the wine info page for each wine in my list. Here’s a simple XSLT file to convert our XML feed to RSS 2.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" indent="yes"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>
<xsl:template match="/">
<rss version="2.0">
<channel>
<title>My Vino2Vino.com RSS Feed</title>
<description>My Vino2Vino.com RSS Feed</description>
<link><xsl:value-of select="/wines/link"/></link>
<xsl:apply-templates select="/wines/wine"/>
</channel>
</rss>
</xsl:template>
<xsl:template match="wine">
<item>
<title>
<xsl:value-of
select="concat(winery, ' ', year, ' ', name)"/>
</title>
<description>
<xsl:value-of select="color"/> wine from
<xsl:value-of
select="concat(region, ', ', region/@country)"/>
</description>
<link>
http://www.vino2vino.com/wine/<xsl:value-of
select="@id"/>
</link>
<guid>
http://www.vino2vino.com/wine/<xsl:value-of
select="@id"/>
</guid>
<source url="{/wines/link}">
<xsl:value-of
select="concat(winery, ' ', year, ' ', name)"/>
</source>
</item>
</xsl:template>
</xsl:stylesheet>
Next, we need to apply the transformation to our XML feed and print the output. The PHP code to perform this operation is trivial, so I won’t bother explaining it. If you want more details, check out the PHP online documentation. Here’s the code:
$xmlFile = 'http://vino2vino.com/feed/wine/mike';
$xslFile = 'rss.xsl';
// Initialize XSLTProcessor by passing it a
// DOMDocument object loaded with XSL file
$xsl = new XSLTProcessor();
$xsl->importStyleSheet(DOMDocument::load($xslFile));
// Load source XML into DOMDocument object
$dom = DOMDocument::load($xmlFile);
print($xsl->transformToXml($dom));
If you place this script on your web server you can point an RSS reader at it and keep an eye on your wine lists! The output from this script looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>My Vino2Vino.com RSS Feed</title>
<description>My Vino2Vino.com RSS Feed</description>
<link>http://vino2vino.com/feed/wine/mike</link>
<ttl>30</ttl>
<item>
<title>Masi 1999 Amarone della Valpolicella
Classico Campolongo di Torbe</title>
<description>unknown wine from Veneto, IT</description>
<link>http://www.vino2vino.com/wine/38681</link>
<pubDate>Tue, 13 Mar 2007 05:54:24 GMT</pubDate>
<guid>http://www.vino2vino.com/wine/38681</guid>
<source url="http://vino2vino.com/feed/wine/mike">
Masi 1999 Amarone della Valpolicella
Classico Campolongo di Torbe
</source>
</item>
...
</channel>
</rss>
I’ve put together a couple of useful transformations in a zip file for you to play with. The zip contains stylesheets to convert XML feeds from Vino2Vino.com into RSS, JSON, and HTML. Check it out!
September 18th, 2007 at 11:49 am
Thank you for this. I have just spent the best part of an hour on another, more techy, blog trying to figure this out with no luck. You explain it perfectly!