Transforming dynamically generated XML to HTML

Generating dynamic XML

Transform an XML document dynamically created by PHP to HTML

PHP can create XML documents on-the-fly (dynamically) and then use its XSLT extension to transform to HTML. This means that data retrieved from a database (in this case MySQL) can be formatted as XML and via an XSL style sheet, to be presented as HTML.

Why do to the trouble of having an extra step, when the HTML presentation can be written as part of your PHP script? You could want to have a strict separation between your content and your design. If you change your design you don't want to edit your code, just edit the style sheet. Syndicating your content as XML makes it easier to re-purpose etc.

The first step is creating the XML document from the data in the database:

PHP Code: mysql-to-xml.php

<?php
header("Content-type: text/xml");
 
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("database",$db) or die ("Unable to connect to database");
 
$query= mysql_query("SELECT * FROM table limit 0, 10"); //select first ten rows from table
// Get number of rows returned.
$rows_found = mysql_num_fields($query);
 
print '<?xml version="1.0"?>';
print '<result>';
 
while($row = mysql_fetch_array($query)){
print "<row>";
for($i=0; $i< $rows_found; $i++){
$tag = mysql_field_name($query,$i);
print "<$tag>". $row[$i]. "</$tag>";
}
print "</row>";
}
print "</result>";
?>

XML file: produced by the PHP script

<?xml version="1.0" ?>
- <result>
- <row>
<id>1</id>
<name>DevShed PHP</name>
<url>http://www.devshed.com/Server_Side/PHP/</url>
<alt>DevShed PHP</alt>
</row>
- <row>
<id>2</id>
<name>PHP club</name>
<url>http://phpclub.unet.ru/index_e.php3</url>
<alt>PHP club</alt>
</row>
- <row>
<id>3</id>
<name>phpWizard.net</name>
<url>http://www.phpwizard.net/</url>
<alt>phpWizard.net</alt>
</row>
- <row>
<id>4</id>
<name>PHPBuilder.com</name>
<url>http://www.phpbuilder.com/</url>
<alt>PHPBuilder.com</alt>
</row>
- <row>
<id>5</id>
<name>Vt. Web Wizard MySQL PHP Tutorial</name>
<url>http://www.vtwebwizard.com/tutorials/mysql/</url>
<alt>Vt. Web Wizard MySQL PHP Tutorial</alt>
</row>
- <row>
<id>6</id>
<name>PHP 3.0 MSQL 2.0 tutorial</name>
<url>http://hagen.let.rug.nl/~s0367672/pm_int_e.htm</url>
<alt>PHP 3.0 MSQL 2.0 tutorial</alt>
</row>
- <row>
<id>7</id>
<name>PHP/MySQL Tutorial</name>
<url>http://hotwired.lycos.com/webmonkey/99/21/index2a.html</url>
<alt>Webmonkey's PHP/MySQL Tutorial</alt>
</row>
- <row>
<id>8</id>
<name>Web mail in PHP</name>
<url>http://www.earthweb.com/</url>
<alt>Web mail in PHP</alt>
</row>
- <row>
<id>9</id>
<name>Database Enabled Websites</name>
<url>http://www.awtrey.com/support/dbeweb/</url>
<alt>Database Enabled Websites</alt>
</row>
- <row>
<id>10</id>
<name>PHP faqts/PHP Knowledge Base</name>
<url>http://www.faqts.com/knowledge-base/index.phtml/fid/51/</url>
<alt>PHP faqts/PHP Knowledge Base</alt>
</row>
</result>

Transforming XML

Creating an XSL stylesheet

XSL file: mysql-to-xslt.xsl to transform XML to HTML

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="utf-8"/>
 
<xsl:template match="/">
<html>
<head>
<title>XSLT Transform from MySQL</title>
</head>
<body bgcolor="ffffff" text="BBBBCC" link="0077CC" vlink="0077CC" alink="FFFFFF">
<table width="300" border="1" cellpadding="0" cellspacing="0">
<xsl:call-template name="rows"/>
</table>
 
</body>
</html>
</xsl:template>
 
<xsl:template name="rows">
<xsl:for-each select="result">
<xsl:for-each select="row">
<tr>
<td width="100%">
<font face="verdana" size="1"><xsl:value-of select="id"/><b><a href="{url}" alt="{alt}" target="_"><xsl:value-of select="name"/></a></b></font><br/>
</td>
</tr>
 
<tr bgcolor="777777"><td></td><td colspan="2"><br/></td></tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
 
</xsl:stylesheet>

The script to perform the transformation then print the result to the browser.

PHP Code:

<?php
function readFile($fileName)
{
// get contents of a file into a string
$fd = fopen( $fileName, "r" );
 
while(!feof($fd)) {
$content .= fread($fd, 4096);
}
 
fclose( $fd );
return $content;
}
 
//path to XSL file is assigned to $xsl and the XML file to $xml
$xml = readFile('mysql-to-xml.php');
$xsl = readFile('mysql-to-xslt.xsl');
 
$arguments = array(
'/_xml' => $xml,
'/_xsl' => $xsl
);
 
// Allocate a new XSLT processor
$xh = xslt_create();
 
// Process the document
$transformation = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
 
print $transformation;
 
xslt_free($xh);
?>