HomePHP Page 8 - Plugging RDF Content Into Your Web Site With PHP
Adding A Little Style - PHP
A Web site which dynamically updates itself with the latestnews and information? Nope, it's not as far-fetched as it sounds. Asthis article demonstrates, all you need is a little imagination, acouple of free RDF files and some PHP glue.
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:
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.