Transformation Using XML, XSL and PHP Query
Posted: Sat Jul 30, 2005 8:57 am
Hi Guys i got a problem with a transformation im trying to do. Im learning about using php to output a xml file using a xsl stylesheet. The code i believe is fine however when i view it in the web browser on the server nothing is displayed or outputted. I've included all the relvent code. Hope someone can help.
Code: Select all
xslt.class.php fileCode: Select all
<?php
class xslt
{
var $xslfile;
var $xmlfile;
var $filename;
//xslt gets the xml and xsl files to perform the transformation
function xslt($xslfile= '', $xmlfile= '')
{
$this->xsl_string = "../xsl/".$xslfile;
$this->xml_string = "../xml/".$xmlfile;
}
function transform()
{
$this->result = '';
$xslHandle = xslt_create(); //create the processor
//pass the xslt_process function the files
$this->result = xslt_process($xslHandle, $this->xsl_string, $this->xml_string);
if(!$this->result)
{
die("XSLT Error:" .xslt_error($xslHandle));
xslt_free($xslHandle);
return $this->result;
}
}
}
?>Code: Select all
transformation.phpCode: Select all
<?php
include_once("../classes/xslt.class.php");
$xsl = new xslt("docbook.xsl", "docbook.xml");
header("Content-type: text/html");
print ($xsl->transform());
?>Code: Select all
xml fileCode: Select all
&lt;?xml version="e;1.0"e; encoding="e;UTF-8"e;?&gt;
&lt;article&gt;
&lt;title&gt;A Sample Article&lt;/title&gt;
&lt;section&gt;
&lt;title&gt;Article Section 1&lt;/title&gt;
&lt;para&gt;
This is the first section of the article. Nothing terribly
interesting here, though.
&lt;/para&gt;
&lt;/section&gt;
&lt;section&gt;
&lt;title&gt;Another Section&lt;/title&gt;
&lt;para&gt;
Just so you can see how these things work, here's an
itemized list:
&lt;/para&gt;
&lt;itemizedlist&gt;
&lt;listitem&gt;
&lt;para&gt;The first item in the list&lt;/para&gt;
&lt;/listitem&gt;
&lt;listitem&gt;
&lt;para&gt;The second item in the list&lt;/para&gt;
&lt;/listitem&gt;
&lt;listitem&gt;
&lt;para&gt;The third item in the list&lt;/para&gt;
&lt;/listitem&gt;
&lt;/itemizedlist&gt;
&lt;/section&gt;
&lt;/article&gt;Code: Select all
XSl StylesheetCode: Select all
&lt;?xml version="e;1.0"e; encoding="e;UTF-8"e;?&gt;
&lt;xsl:stylesheet version="e;1.0"e;
xmlns:xsl="e;http://www.w3.org/1999/XSL/Transform"e;&gt;
&lt;xsl:output method="e;html"e;/&gt;
&lt;xsl:template match="e;article"e;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;xsl:value-of select="e;title"e; /&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;
&lt;xsl:value-of select="e;title"e; /&gt;
&lt;/h1&gt;
&lt;xsl:apply-templates select="e;section"e; /&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="e;section"e;&gt;
&lt;xsl:apply-templates/&gt;
&lt;hr/&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="e;section/title"e;&gt;
&lt;h2&gt;&lt;xsl:apply-templates/&gt;&lt;/h2&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="e;para"e;&gt;
&lt;p&gt;&lt;xsl:apply-templates/&gt;&lt;/p&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="e;itemizedlist"e;&gt;
&lt;ul&gt;&lt;xsl:apply-templates/&gt;&lt;/ul&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="e;listitem"e;&gt;
&lt;li&gt;&lt;xsl:apply-templates/&gt;&lt;/li&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;