playing with xml and xsl translations...

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

playing with xml and xsl translations...

Post by timvw »

Ok, i'm experimenting as following: Code that retrieves data from a database, wraps the data in XML and then generate XHTML using XSL Transformation. The following code should get you started: (it's a shame that php still doesn't have utf-8 support by default. Requires that you have the XSL extension (sablotron/php4) and the mb_string extension enabled.)

Code: Select all

<?php
// +---------------------------------------------------------------------------
// | index.php
// |
// | Author: Tim Van Wassenhove<timvw@users.sourceforge.net>
// | Update: 2005-01-19 02:47
// |
// | Display the last 5 messages.
// +---------------------------------------------------------------------------

// debugging time
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

// need some libraries
require_once('mysql.php');
require_once('rstoxml.php');
require_once('transform.php');
require_once('output.php');

// perform the query
$query = "SELECT * FROM message ORDER BY datetime DESC LIMIT 0, 5";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);

// wrap xml around the returned data in the resultset
$xml = rstoxml($result);

// load the xsl translation, assume it is in xsl/index.sl 
$xsl = file_get_contents('xsl/index.xsl');

// perform the translation
$xhtml = transform($xml, $xsl);

// output the translated html
output($xhtml);
?>

Code: Select all

<?php

// +---------------------------------------------------------------------------
// | rstoxml.php
// | 
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
// | Update: 2005-01-19 02:47
// |
// | Transform the elements of a resultset to an XML document. 
// +---------------------------------------------------------------------------

function rstoxml($result)
{
    $doc = domxml_new_doc('1.0');
    $root = $doc->create_element('resultset');
    $root = $doc->append_child($root);

    while($row = mysql_fetch_assoc($result)) 
    {
        // add node for each row
        $occ = $doc->create_element('entity');
        $occ = $root->append_child($occ);

        // add a child node for each field
        foreach ($row as $fieldname => $fieldvalue) 
        {
            $child = $doc->create_element('attribute');
            $child->set_attribute('name', $fieldname);
            $child = $occ->append_child($child);
            $fieldvalue = mb_convert_encoding($fieldvalue,'UTF-8', 'ISO-8859-1');
            $value = $doc->create_text_node($fieldvalue);
            $value = $child->append_child($value);
        } 
    } 
    $xml = $doc->dump_mem(true, 'ISO-8859-1');
    return $xml;
}
?>

Code: Select all

<?php

// +---------------------------------------------------------------------------
// | transform.php
// | 
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
// | Update: 2005-01-19 02:47
// |
// | Perform the given XSL Translation on the given XML. 
// +---------------------------------------------------------------------------

function transform($xml, $xsl, $params = NULL)
{
    $xp = xslt_create() or trigger_error('Could not create XSLT process.', E_USER_ERROR);
    xslt_set_encoding($xp, 'UTF-8');
    xslt_set_base($xp, 'file://' . dirname(__FILE__) . '/xsl/');
    $args = array('/xml' => $xml, '/xsl' => $xsl);
    $result = xslt_process($xp, 'arg:/xml', 'arg:/xsl', NULL, $args, $params);
    if (!$result)
    {
        trigger_error('XSLT error: ' . xslt_error($xp) . '(error code: ' . xslt_errno($xp) . ')');
        $result = $xml;
    }
    xslt_free($xp);
    return $result;
}
?>

Code: Select all

<?php

// +---------------------------------------------------------------------------
// | output.php
// | 
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
// | Update: 2005-01-19 02:47
// |
// | Output the given html in utf-8
// +---------------------------------------------------------------------------

function output($html)
{
    header('content-type:text/html; charset=UTF-8');
    header('Expires: Mon, 14 Oct 2002 05:00:00 GMT');
    header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header('Pragma: no-cache');
    echo $html;
}
?>

Code: Select all

<?php
$db = mysql_connect('localhost', 'user', 'password') or trigger_error(mysql_error(), E_USER_ERROR);
mysql_select_db('test', $db) or trigger_error(mysql_error(), E_USER_ERROR);
?>

Code: Select all

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method='xhtml'
            indent="yes"
            omit-xml-declaration="yes"
            doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN"
            doctype-system = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>

<xsl:template match="/">

<html xml:lang="en" lang="en">
<head>
<title>Index</title>
<link rel="stylesheet" href="/styles/style.css" type="text/css"/>
</head>

<body>

<xsl:for-each select="//resultset/entity">
  <div class="mainitem">
    <div class="maintitle"><xsl:value-of select="attribute@name='title']"/></div>
    <div class="maininfo"><xsl:value-of select="attribute@name='timestamp']"/></div>
    <div class="maincontent"><xsl:value-of select="attribute@name='content']"/></div>
  </div>
</xsl:for-each>

</body>
</html>

</xsl:template>

</xsl:stylesheet>
Post Reply