The Sablotron extension provides PHP scripts with the ability to apply XSLT stylesheets to XML data in a variety of ways. In order to have access to these functions in PHP, you need to compile PHP with the --with-sablot=[DIR] configure option. The Sablotron library can be downloaded from http://www.gingerall.com.
For FAQs, tutorials, and specifications regarding XML and XSL, two good places to start are http://xml.coverpages.org and http://www.xml.org.
There are various levels at which one may use the Sablotron functionality. Perhaps the easiest allows you to tell PHP to apply a stylesheet to all output between two function calls: anything which PHP outputs between a call to xslt_outputbegintransform() and xslt_outputendtransform() will have the requested stylesheet applied to it. The second, slightly lower-level method, involves putting your stylesheet into one string, your XML data into another, and passing both strings to xslt_process() ; the resulting document will be placed into a third string parameter. Another method also allows you to apply a stylesheet to an XML document with a single function call. Using xslt_transform() , you supply the URIs of a stylesheet file, an XML document file, and a result buffer, plus optionally some parameters and arguments. A result buffer is a section of Sablotron's internal memory which is accessible via a name using the Sablotron-specific 'arg:' URI scheme (see below for more information on supported URI schemes). With most of these functions, if no buffer URI is given, a default result buffer name of 'arg:/_result' will be used. The fourth, lowest-level method is suitable, for instance, when you need to use Sablotron's logging functionality. In this method, you create a parser with xslt_create() , set any logging (if needed) with xslt_openlog() , and then apply the transformation itself with xslt_run() . The results then need to be retrieved using xslt_fetch_result() .
Sablotron currently supports two URI schemes: 'file:' for files which can be found on the server filesystem, and 'arg:' for naming internal document buffers. Under Windows environments, if you need to supply a drive letter as part of a 'file:' URI, the format must follow the general syntax of 'file://c:/path/to/document.xml'. In other cases, the 'file:' is optional.
For the examples provided, you can download the file located at http://www.slashdot.org/slashdot.xml for use as the XML document. The simple XSL used to test these examples is provided below; for the purpose of testing it was placed in the file slashdot.xsl in the same directory as the PHP scripts and XML file.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:param name="generated-for"></xsl:param> <xsl:template match="/"> <xsl:if test="$generated-for"> <h3>Generated for: <xsl:value-of select="$generated-for"></xsl:value-of></h3> </xsl:if> <xsl:for-each select="//story"> <A> <xsl:attribute name="HREF"><xsl:value-of select='url' /></xsl:attribute> <xsl:attribute name="NAME">testname</xsl:attribute> <xsl:value-of select="title"/> </A> By <xsl:value-of select="author"/> at <xsl:value-of select="time"/> <HR/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.