note 14547 deleted from function.xslt-run by nlopess
| From: | nlopess@php.net | Date: | Sat, 26 Jun 2004 19:07:28 +0000 |
| Subject: | note 14547 deleted from function.xslt-run by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71802@lists.php.net to get a copy of this message | ||
Note Submitter: regenfeld@gmx.net
----
A quick and dirty XSLT-Class using xslt_run() for passing Parameters to an xsl-stylesheet:
class XSLT {
var $xml_file;
var $xsl_file;
var $output;
var $processor;
var $error = array();
var $params = array();
var $args = array();
function XSLT($xsl="", $xml="") {
$this->processor = xslt_create();
if (isset($xsl)) { $this->xsl_file = $xsl;}
if (isset($xml)) { $this->xsl_file = $xml;}
}
function destroy() {
xslt_free($this->processor);
}
function Output(){
return $this->output;
}
function setXmlFile($uri) {
$this->xml_file = $uri;
}
function setXslFile($uri){
$this->xsl_file = $uri;
}
function XSLTransformation() {
if (xslt_run($this->processor, $this->xsl_file, $this->xml_file,
"arg:/_result", $this->params, $this->args)) {
$this->output = xslt_fetch_result($this->processor);
return true;
} else {
$this->output = "xslt_run(): Error!";
return false;
}
}
function setParam($key, $value){
$this->params[$key] = $value;
}
}
USAGE:
------
$xsl = "file://C:/Apache/htdocs/file.xsl";
$xml = "file://C:/Apache/htdocs/file.xml";
$xslt = new XSLT();
$xslt->setParam("p","Test1");
$xslt->setParam("q","Test2");
$xslt->setXmlFile($xml);
$xslt->setXslFile($xsl);
$xslt->XSLTransformation();
echo $xslt->Output();
XML-Example:
------------
<?xml version="1.0" ?>
<doc>Here is a short test:</doc>
XSL-Example:
------------
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="p"/>
<xsl:param name="q"/>
<xsl:template match="/">
<xsl:value-of select="doc" /><br />
p: <xsl:value-of select="$p" /><br />
q: <xsl:value-of select="$q" />
</xsl:template>
</xsl:stylesheet>