The Ground Cero Guide to XSL
Henrik Aasted Sorensen
<< More XSLTGoodbye >>
8. XSL-processors
A number of XSL-processors exist. My experience lies in two of them: Internet Explorer 6 and the XSL-library of PHP.
Internet Explorer 6
IE6 has (reputedly good) support for XSL and XML-documents in general. If it receives an XML-document in a language it doesn't know, it will simply display it. I find that IE6 is very good for debugging stylesheets and documents, because it produces rather detailed and exact reports in case of an error in the documents. The problem is that IE6 is a rather new browser and it's not in as widespread use. It's therefore a bit hard to rely on the browser being able to do the XSL-transformation. The solution to this is performing the transformation on the server instead, and just send the result to the browser.
Support for doing this can be found in a lot of server-languages. I'll make short introduction to using the PHP-version in the following.
PHP
Some PHP-servers come with the XSLT-library. It's unfortunately not a standard part of the PHP-package. The three most important functions in the library are:
 xslt_create() 
This method creates a handle to an XSL-processor. It takes no arguments.
xslt_process(XSL-handle, XML-FILE, XSL-file)
This method is doing the actual transformation. The first argument is the handle obtained from the previous function. The second and third arguments are the filename of the XML-file and XSL-file respectively. The resulting XML-document is returned from the function call.
xslt_free(XSL-handle);
This function releases the resources allocated in connection with the xslt_create()-function.
Passing parameters
It's possible to pass parameters to the XSL-document before the transformation. This can be done through the extended version of the xslt_process()-function
xslt_process(XSL-handle, XML-file, XSL-file, Returnvalue ,Argument-array,Parameter-array);
The first three parameters were described above. Returnvalue gives the possibility of getting the resulting XML- document. The Argument-array can be used if the XML- and XSL-documents are not kept in files, but are instead kept in a variable. The fifth argument is an associative array containing the XML- and XSL-documents. The parameters XML-file and XSL-file must be substituted with "arg:/index", where index is the key of the document in the associative array. Parameter-array is also an associative array. Each variable's index must match an <xsl:param> in the XSL-document.
The following is an example of an XSL-transformation in PHP. The XML- and XSL-data are considered to be contained in the variables $xml and $xsl.
    // Create a new processor handle
    $th = xslt_create() or die("Can't create XSLT handle!");
    
    $args = array();
    $params = array();
    
    $args["xml"] = $xml;
    $args["xsl"] = $xsl;
    
    if (isset($cat)) {
        $params["cat"] = $cat;
    }
    
    // Perform the XSL transformation
    $trans = xslt_process($th, "arg:/xml", "arg:/xsl", null,$args,$params);
    if (!$trans) {
        print "Failure: Reason is that " . xslt_error($th) . " and the ";
        print "error code is " . xslt_errno($th);
    
    }
        
    echo $trans; // Output the transformed XML file
                        
    xslt_free($th); // Free up the resources
<< More XSLTGoodbye >>
Index