Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Plugging RDF Content Into Your Web Site With PHP
  2. Have Content, Will Syndicate
  3. Switching Channels
  4. Fresh Meat
  5. Capture The Flag
  6. Nesting Time
  7. Back To Class
  8. Adding A Little Style
  9. Homework
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 19
February 27, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: