PHP Sablotron Wrapper Class
Steven Liu's Sablotron extension brings XSLT to PHP on Apache. I've contributed a pair of PHP classes for implementing XSLT transforms using the Sablotron module. Assuming you've installed Sablotron and the PHP extensions, transforming XML with XSL is straightforward.
This code is deprecated. Use the sablotron interface in PHP 4.2.x instead. No support for this code will be provided.
Updates and Corrections
14 November 2001: Steven's website is not responding. I've made a local copy of the Sablot Classes available. However, I suggest that you look at the 'baked-in' Sablotron in PHP 4.0. Urs Gehrig has adapted my classes to work with it.
11 October 2000: I'm checking to see if Steven Liu's going to continue to support his version of the Sablotron extensions since PHP's CVS tree has incorporated a version.
11 October 2000: Jakob Fix writes "You are using exclusively the FileReader object to retrieve an XML file. However, Filereader only gets the file you are asking for, but an XML file might be modular, and therefore have entities in it that should be expanded for that a complete, wellformed or valid document is returned."
He contributed this code to replace the setXML() method:
function setXml($uri)
{
$x = `/usr/local/bin/rxp $uri`;
if (strlen($x) > 0 && is_string($x))
{
$this->xml = $x;
$result = true;
}
else
{
$this->setError("$uri is no valid xml file ...");
$result = false;
}
return $result;
}
Notice that you need to have your own parser available, Jacob uses rxp in this instance.
30 August 2000: In the usage section I used print $string; instead of print $transform->getOutput();
Usage
<?php
include ('fileReader.inc' );
include ('xslTransform.inc');
$transform = new xslTransform();
$transform->setXsl("transform.xsl");
$transform->setXml("data.xml");
$transform->setParameter("foo","bar");
$transform->apply();
if($transform->getError() == 0)
{
print $transform->getOutput();
}
else
{
echo "<p class=\"warning\">",$transform->getErrorstr(),"</p>";
}
?>
Installing
Place fileReader.inc and xslTransform.inc in your PHP include path.
Public Methods
setXSL(str fileURL), setXML(str fileURL)
$transform->setXSL("foo.xsl");
$transform->setXML("http://www.foo.ba/baz.xml");
Assign the URI of a file to the role of either XSL or XML. If the argument doesn't start with 'http:' the method assumes it's a local file and will look for it in your PHP include paths unless you give a full path to it.
This method supports http or files. Any http URL must be less than a megabyte in size.
setParameter(str parameterName, str parameterValue), deleteParameter(str parameterName)
$transform->setParameter("USER","Joe Smith");
$transform->deleteParameter("USER");
This sets a top level parameter available to the stylesheet. You must, of course, declare this parameter as a top level element in your stylesheet using xsl:param.
At this time you cannot pass a nodeset as a parameter, only strings.
getError, getErrorstr
if($transform->getError() == 0)
{
print $string;
}
else
{
echo "<p class=\"warning\">",$transform->getErrorstr(),"</p>";
}
When a transform is applied, the error number is set. If it is non-zero, there was an error applying the transform. The terse descriptive string may be fetched via getErrorstr.
apply
Applies the transform from setXSL to the XML data assigned in setXML with any top-level parameters assigned by setParameter. Use getOutput to get the transformed XML as a string.
getOutput
Call after apply to assign the result of the transform to a string.
finalize
Call this before destroying the object.
Notes
The methods startLogging and stopLogging in the source do nothing.
This code is distributed under a GNU public license.