Page 1 of 1

Transformation Using XML, XSL and PHP Query

Posted: Sat Jul 30, 2005 8:57 am
by mmc01ms
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 file

Code: 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.php

Code: 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 file

Code: Select all

&amp;lt;?xml version=&quote;1.0&quote; encoding=&quote;UTF-8&quote;?&amp;gt; 
&amp;lt;article&amp;gt; 
 &amp;lt;title&amp;gt;A Sample Article&amp;lt;/title&amp;gt; 
 &amp;lt;section&amp;gt; 
   &amp;lt;title&amp;gt;Article Section 1&amp;lt;/title&amp;gt; 
   &amp;lt;para&amp;gt; 
   This is the first section of the article. Nothing terribly 
   interesting here, though. 
   &amp;lt;/para&amp;gt; 
 &amp;lt;/section&amp;gt; 
 &amp;lt;section&amp;gt; 
   &amp;lt;title&amp;gt;Another Section&amp;lt;/title&amp;gt; 
   &amp;lt;para&amp;gt; 
   Just so you can see how these things work, here's an 
   itemized list: 
   &amp;lt;/para&amp;gt; 
   &amp;lt;itemizedlist&amp;gt; 
     &amp;lt;listitem&amp;gt; 
       &amp;lt;para&amp;gt;The first item in the list&amp;lt;/para&amp;gt; 
     &amp;lt;/listitem&amp;gt; 
     &amp;lt;listitem&amp;gt; 
       &amp;lt;para&amp;gt;The second item in the list&amp;lt;/para&amp;gt; 
     &amp;lt;/listitem&amp;gt; 
     &amp;lt;listitem&amp;gt; 
       &amp;lt;para&amp;gt;The third item in the list&amp;lt;/para&amp;gt; 
     &amp;lt;/listitem&amp;gt; 
   &amp;lt;/itemizedlist&amp;gt; 
 &amp;lt;/section&amp;gt; 
&amp;lt;/article&amp;gt;

Code: Select all

XSl Stylesheet

Code: Select all

&amp;lt;?xml version=&quote;1.0&quote; encoding=&quote;UTF-8&quote;?&amp;gt; 
&amp;lt;xsl:stylesheet version=&quote;1.0&quote; 
	xmlns:xsl=&quote;http://www.w3.org/1999/XSL/Transform&quote;&amp;gt; 
	
	&amp;lt;xsl:output method=&quote;html&quote;/&amp;gt; 
	
	&amp;lt;xsl:template match=&quote;article&quote;&amp;gt;
		&amp;lt;html&amp;gt;
			&amp;lt;head&amp;gt;
				&amp;lt;title&amp;gt;&amp;lt;xsl:value-of select=&quote;title&quote; /&amp;gt;&amp;lt;/title&amp;gt;
			&amp;lt;/head&amp;gt;
			&amp;lt;body&amp;gt;
				&amp;lt;h1&amp;gt;
					&amp;lt;xsl:value-of select=&quote;title&quote; /&amp;gt;
				&amp;lt;/h1&amp;gt;
				&amp;lt;xsl:apply-templates select=&quote;section&quote; /&amp;gt;
			&amp;lt;/body&amp;gt;	
		&amp;lt;/html&amp;gt;
	&amp;lt;/xsl:template&amp;gt;
	
		
	&amp;lt;xsl:template match=&quote;section&quote;&amp;gt; 
		&amp;lt;xsl:apply-templates/&amp;gt; 
		&amp;lt;hr/&amp;gt; 
	&amp;lt;/xsl:template&amp;gt; 
	
	&amp;lt;xsl:template match=&quote;section/title&quote;&amp;gt; 
		&amp;lt;h2&amp;gt;&amp;lt;xsl:apply-templates/&amp;gt;&amp;lt;/h2&amp;gt; 
	&amp;lt;/xsl:template&amp;gt; 
	
	&amp;lt;xsl:template match=&quote;para&quote;&amp;gt; 
		&amp;lt;p&amp;gt;&amp;lt;xsl:apply-templates/&amp;gt;&amp;lt;/p&amp;gt; 
	&amp;lt;/xsl:template&amp;gt; 
	
	&amp;lt;xsl:template match=&quote;itemizedlist&quote;&amp;gt; 
		&amp;lt;ul&amp;gt;&amp;lt;xsl:apply-templates/&amp;gt;&amp;lt;/ul&amp;gt; 
	&amp;lt;/xsl:template&amp;gt; 
	
	&amp;lt;xsl:template match=&quote;listitem&quote;&amp;gt; 
		&amp;lt;li&amp;gt;&amp;lt;xsl:apply-templates/&amp;gt;&amp;lt;/li&amp;gt; 
	&amp;lt;/xsl:template&amp;gt; 
&amp;lt;/xsl:stylesheet&amp;gt;

Posted: Sat Jul 30, 2005 9:27 am
by timvw
You only return $this-&gt;result when there is an error... So, php default returns NULL if you have no errors...

Code: Select all

if(!$this->result)
{
  die("XSLT Error:" .xslt_error($xslHandle));
  xslt_free($xslHandle);
  return false; 
}
return $this->result;

Posted: Sun Jul 31, 2005 10:09 am
by mmc01ms
thanks for the reply and it worked however the output i get is not corrosponding to the xsl stylesheet correctly. The output is ok but the html is not being outputed. I though the parser ignored parsing this and sent it out anyway? this si the html code i get returned from my transformation.php page. Could you please shed some light on this for me? Cheers

Code: Select all

<?xml version=&quote;1.0&quote; encoding=&quote;UTF-8&quote;?>
<article><title>A Sample Article</title><section>
<title>Article Section 1</title>
<para> 
   This is the first section of the article. Nothing terribly 
   interesting here, though. 
   </para>
</section><section><title>Another Section</title>
<para> 
   Just so you can see how these things work, here's an 
   itemized list: 
   </para>

<itemizedlist><listitem><para>The first item in the list</para></listitem><listitem><para>The second item in the list</para></listitem><listitem><para>The third item in the list</para></listitem></itemizedlist>
</section></article>

Posted: Sun Jul 31, 2005 10:27 am
by timvw
That is because your XSL doesn't do what you expect it to do...