[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PHP Sablotron Wrapper Class



 
    
 
http://www.whump.com/www/phpSablot.html
 
Title: PHP Sablotron Wrapper Class
whump dot com

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.

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 $string;
}
else
{
	echo "<p class=\"warning\">",$transform->getErrorstr(),"</p>";
}

?>

Installing

Download.

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.


Last modified on 8/7/00; 11:23:27 PM.
Contact WHUMP dot COM.
Copyright © 1995-2000, Bill Humphries
Google