#native_company# #native_desc#
#native_cta#

Transforming XML with XSL using Sablotron Page 2

By Justin Grant
on October 24, 2000

The basic Sablotron extension module functions available to PHP4 are listed
and briefly explained below :
PHP4
Sablotron extension module v0.21 functions
 
$processor = xslt_create_processor();
Creates and returns an
instance of the Sablotron XSLT Processor
 
xslt_destroy_processor($processor);
Destroys the specified
instance of the Sablotron XSLT Processor ($processor)
 
$strResult = xslt_process($processor,$sheetURI,
$inputURI, $resultURI, $arrayParams, $arrayArguments, $bResultString,[[$errorcode],[$errormsg]]);
Using an instance of
the processor($processor) this function processes an XML document($inputURI)
against an XSL document($sheetURI). A resulting document can be specified
($resultURI) or NULL can be substituted if no output document is
required to be written. XSL parameters ($arrayParams) and XSL arguments
($arrayArguments) can be passed if required or NULL must be specified.
A boolean ($bResultString) indicates wether or not the result must
be returned as a string. Optional error code ($errorcode) and error
message ($errormsg) variables can be specified.
 
$strResult = xslt_process_strings($processor,$strStyleSheet,$strInput,[[$errorcode],[$errormsg]]);
Wraps the
xslt_process() function except that the XSL stylesheet and XML document
are passed as strings to the processor.
 
$strResult = xslt_process_files($processor,$StyleSheetfile,$Inputfile,$Outputfile,[[$errorcode],[$errormsg]]);
Wraps the
xslt_process() function except that the XSL stylesheet and XML document
are passed as file paths to the processor.
 

$strResult = xslt_process_stringswithbase($processor,$strStyleSheet,$strInput,$strHardBase,
[[$errorcode],[$errormsg]]);

The same as xslt_process_strings()
except that the base encoding ($strHardBase) can be specified.
 
$strErrorMsg = xslt_get_error_msg($processor);
Returns the error message
generated by the XSLT Processor ($processor)
 
$bOK = xslt_openlog($processor,$logfilename,[$loglevel]);
Opens a log file ($logfilename)
with the optional parameter for specifiying the log level ($loglevel)
to log the progress of the processor ($processor). Returns a boolean
($bOK) indicating if the call was successful.
 
$bOK = xslt_closelog($processor);
Closes the
log file in use by the processor ($processor). Returns a boolean
($bOK) indicating if the call was successful.
 

 

This table is provided as a reference so that later the implementation of the
example class that wraps these functions can be better understood. You will
notice later on that I only use four of these functions in my class, namely
xslt_create_processor(), xslt_destroy_processor(),
xslt_process_files() and xslt_get_error_msg().

 

Let’s look at a sample XML document that Slashdot publishes on their site and
that I will transform using XSLT:
<?xml version="1.0"?>
<backslash xmlns:backslash="http://slashdot.org/backslash.dtd">
 <story>
         <title>Free Stripped-Down 3D Studio Max</title>
         <url>http://slashdot.org/article.pl?sid=00/07/30/2056209</url>
         <time>2000-07-30 20:54:56</time>
         <author>timothy</author>
         <department>introduce-me-to-some-models-please</department>
         <topic>games</topic>
         <comments>105</comments>
         <section>articles</section>
         <image>topicgames.jpg</image>
 </story>
 <story>
         <title>Preliminary Ethereal User's Guide</title>
         <url>http://slashdot.org/article.pl?sid=00/07/30/168207</url>
         <time>2000-07-30 17:06:57</time>
         <author>CmdrTaco</author>
         <department>docs-for-cool-software</department>
         <topic>news</topic>
         <comments>157</comments>
         <section>articles</section>
         <image>topicnews.gif</image>
 </story>
</backslash>
This is quite a self-explanatory XML document. It obviously contains story
data that you might find on the Slashdot web site. The <story> element
contains child elements (<title>, <url>, <time>, <author>,
<department>, <topic>, <comment>, <section> and <image>)
that contain data we can format how ever we like using the power of XSL.