#native_company# #native_desc#
#native_cta#

Transforming XML with XSL using Sablotron Page 5

By Justin Grant
on October 24, 2000

<?php

/*

    XSLTranformer -- Class to transform XML files using 

    XSL with the Sablotron libraries.Justin Grant (2000-07-30)

    Thanks to Bill Humphries for the original examples on 

    using the Sablotron module.

*/

/* test */

/*

$transformer = new XSLTransformer();

if ($transform->setXsl("http://www.someurl.com/document.xsl") &&

    $transform->setXml("http://www.someurl.com/document.xml")) {

       $transformer->transform();

       echo $transformer->getOutput();

    } else {

       echo $transformer->getError();

    }

*/

class XSLTransformer {

   var 
$xsl$xml$output$error ;

/* Constructor */

   
function XSLTransformer() {

      
$this->processor xslt_create_processor();

   }

 
/* Destructor */

   
function destroy() {

      
xslt_destroy_processor($this->processor);

   }

 
/* output methods */

   
function setOutput($string) {

      
$this->output $string;

   }

   function 
getOutput() {

      return 
$this->output;

   }

 
/* set methods */

   
function setXml($uri) {

      if(
$doc = new docReader($uri)) {

         
$this->xml $doc->getString();

         return 
true;

      } else {

         
$this->setError("Could not open $xml");

         return 
false;

      }

   }

   function 
setXsl($uri) {

      if(
$doc = new docReader($uri)) {

         
$this->xsl $doc->getString();

         return 
true;

      } else {

         
$this->setError("Could not open $uri");

         return 
false;

      }

   }

/* transform method */

   
function transform() {

      
$this->setOutput(

      
xslt_process_strings($this->processor,

      
$this->xsl$this->xml, &$err));

      
$this->setError($err);

   }

/* Error Handling */

   
function setError($string) {

      
$this->error $string;

   }

   function 
getError() {

      return 
$this->error;

   }

}

/* docReader -- read a file or URL as a string */

/* test */

/*

   $docUri = new docReader('http://www.someurl.com/doc.html');

   echo $docUri->getString();

*/

class docReader {

   var 
$string// public string representation of file

   
var $type// private URI type: 'file','url'

   
var $bignum 1000000;

 
/* public constructor */

   
function docReader($uri) { // returns integer      $this->setUri($uri);

      
$this->setType();

      
$fp fopen($this->getUri(),"r");

      if(
$fp) { // get length

         
if ($this->getType() == 'file') {

            
$length filesize($this->getUri());

         } else {

            
$length $this->bignum;

         }

      
$this->setString(fread($fp,$length));

         return 
1;

      } else {

         return 
0;

      }

   }

 
/* determine if a URI is a filename or URL */

   
function isFile($uri) { // returns boolean

      
if (strstr($uri,'http://') == $uri) {

         return 
false;

      } else {

         return 
true;

      }

   }

 
/* set and get methods */

   
function setUri($string) {

      
$this->uri $string;

   }

   function 
getUri() {

      return 
$this->uri;

   }

   function 
setString($string) {

      
$this->string $string;

   }

   function 
getString() {

      return 
$this->string;

   }

   function 
setType() {

      if (
$this->isFile($this->uri)) {

         
$this->type 'file';

      } else {

         
$this->type 'url';

      }

   }

   function 
getType() {

      return 
$this->type;

   }

}

?>



The class definition for XSLTransformer is a little more involved but not much,
most PHP’ers should be able to gain a good understanding of how to use Sablotron’s
power from PHP by going over this code carefully.
In case you were wondering, there actually are two classes defined in this
file. The first is XSLTansformer ofcourse and the second is docReader. docReader
allows me to read a document from a URL or a file.
The methods meant for public use in the XSLTransformer class can be briefly
explained as follows:
XSLTransformer()
This is the PHP constructor and creates an instance of the Sablotron processor
when an instance of itself is created.
destroy()
This is the method to call when finished using the processor. It destroys the
current instance of the Sablotron processor being used.
getOutput()
Returns the result of a transform.
setXML() and setXSL()
Allows you to load the XML and XSL from a file or URL. If an error occurs they
set the class error property.
transform()
Performs the transform on the XML document. Sets the error property if an
error occurs.
getError()
Returns in error that may have occured during loading of a document or performing
a transform.
The other methods are used by the class internally and cannot be encapsulated
because PHP4 does not support this yet.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Justin Grant

Comment:



Comment: