logo of blechtrottel.net blechtrottel.net
deutsch

XSLT in PHP4

How to do it

All functions listed need to get the paths to the XML and XSL files. In order to guarantee that on on each and every webspace you first of all have to find the root directory and save its path into a variable.

<?php
$filepath = $_SERVER['DOCUMENT_ROOT'];
?>

To this variable we add the relative path to the files.

libxslt

XSLT with libxslt looks as follows:

<?php

$domXmlObj = domxml_open_file($filepath . '/rss.xml');

$domXsltObj = domxml_xslt_stylesheet_file($filepath . '/rss2html.xsl');

$domTranObj = $domXsltObj->process($domXmlObj);

echo $domXsltObj->result_dump_mem($domTranObj);

?>

In the first line the XML file is read, in the second the same happens to the stylesheet. After that the former is transformed by the latter. Finally the result is printed. This method gets you not only utf-8 but also iso encodings, depending on the source files.

In PHP5 you can find libxslt as well; it is our favourite method even there. So if you migrate from PHP4 to PHP5 you need not necessarily rewrite your scripts.

Sablotron

For the Sablotron parser the code looks as follows:

<?php

$zeiger = xslt_create();

$html = xslt_process($zeiger, $filepath . '/rss.xml',
        $filepath . '/rss2html.xsl');

xslt_free($zeiger);

print $html;

Here it is not so easy to follow what is happening. We advise copy-and-paste.

Warning: Regardless of the source encoding Sablotron invariably outputs utf-8.

Sablotron is not a very popular parser for XSLT and no longer standard in PHP5. It often shows odd behaviour and is not really fast, either. Our webspace provider here at blechtrottel brodaktschns decided, without our enquiring, to move our pages to PHP5 in order to avoid the recurring problems with Sablotron. (Thanks go to the chaps at Viennaweb.)

Page 1 - Page 2 - Page 3 - Page 4