Plugging RDF Content Into Your Web Site With PHP - Adding A Little Style
(Page 8 of 9 )
In case you don't like the thought of iterating through all those PHP arrays and marking them up with HTML, you also have the option to format and display the data using an XSLT stylesheet. PHP 4.1 comes with support for the Sablotron XSLT processor via a new XSLT API, which can be used to combine an XSLT stylesheet with an XML (or, in this case, RDF) document to easily transform XML markup into browser-readable HTML.
I'm not going to get into the details of this - take a look at the PHP manual, or at the links at the end of the article, for detailed information - but I will demonstrate what I mean with a simple example. First, here's the stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rss="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0">
<!-- main page -->
<xsl:template match="/rdf:RDF">
<html>
<head>
<basefont face="Arial" size="2"/>
</head>
<body>
<xsl:apply-templates select="rss:channel" />
<ul>
<xsl:apply-templates select="rss:item" />
</ul>
</body>
</html>
</xsl:template>
<!-- channel -->
<xsl:template match="rss:channel">
<b>
<a>
<xsl:attribute name="href"><xsl:value-of select="rss:link"
/></xsl:attribute>
<xsl:value-of select="rss:title" />
</a>
</b>
</xsl:template>
<!-- item -->
<xsl:template match="rss:item">
<li />
<a>
<xsl:attribute name="href"><xsl:value-of select="rss:link"
/></xsl:attribute>
<xsl:value-of select="rss:title" />
</a>
<br />
<xsl:value-of select="rss:description" />
</xsl:template>
</xsl:stylesheet>
And here's the PHP script that combines the stylesheet above
with the Freshmeat RDF document described previously to generate an HTML page:
<?php
// XML file
// this needs to be a local file
$xml = "fm-releases.rdf";
// XSLT file
$xslt = "fm.xsl";
// create a new XSLT processor
$xp = xslt_create();
// transform the XML file as per the XSLT stylesheet
// return the result to $result
$result = xslt_process($xp, $xml, $xslt);
if ($result)
{
// print it
echo $result;
}
// clean up
xslt_free($xp);
?>
Fairly simple and self-explanatory again, I think. The two
documents are merged to produce the following composite output:

This is an alternative, and perhaps simpler (though not all that optimal), method of turning RDF data browser-readable HTML. Note, however, that you will need to have an external program running (probably via cron) to update your local copy of the RDF file on a regular basis, since the PHP XSLT processor may have trouble accessing a remote file.
Next: Homework >>
More PHP Articles
More By icarus, (c) Melonfire