«^»
5.2. Creating a tool to create HTML from XML and XSL

Instead of sprinkling the HTML we want to generate all over a PHP program, we could instead use the XSL stylesheet that was produced in Section 3.

PHP provides an extension that can do XSL transformations. In order to use this extension, the person installing PHP needs to build it with a library that implements XSLT. Currently, the only library supported by PHP is the Sablotron library, but future versions of PHP will support other libraries (such as Xalan and libxslt).

No matter what library for XSLT has been included, it will be used in the same way. Its use is demonstrated by the following program.

<%
   $xsltproc = xslt_create();
   $html = xslt_process($xsltproc,"consumables.xml","consumables.xsl");
   if (!$html) {
      die("XSLT processing error:" . xslt_error($xsltproc));
   }
   xslt_free($xsltproc);
   echo $html;
%> 

The crucial statement of this program is:

$html = xslt_process($xsltproc,"consumables.xml","consumables.xsl");
The call of the xslt_process function takes the file consumables.xml and transforms it using the rules in the file consumables.xsl. This will produce a long string of HTML instructions, and this is stored in the $html variable. In the above program, the statement:
echo $html;
causes this HTML to be output.