HomeXML Page 2 - XSL Transformation With PHP And Sablotron
Getting Down To Business - XML
So you've got your XML, and you've also got an XSLT stylesheet to format it. But how do you put the two of them together? Fear not - you can use PHP's Sablotron extension to perform XSLT transformation of XML data on the server. This article tells you how.
Before we get into the nitty-gritty of XSLT processing with PHP, I'd like to take some time to explain how all the pieces fit together.
In case you don't already know, XML is a markup language created to help document authors describe the data contained within a document. This description is accomplished by means of tags, very similar in appearance to regular HTML markup. However, where HTML depends on pre-defined tags, XML allows document authors to create their own tags, immediately making it more powerful and flexible. There are some basic rules to be followed when creating an XML file, and a file can only be processed if these rules are followed to the letter.
Once a file has been created, it needs to be converted, or "transformed", from pure data into something a little more readable. XSLT, the Extensible Style Language (Transformations), is typically used for such transformations; it's a powerful language that allows you to generate different output from the same XML data source. For example, you could use different XSL transformations to create an HTML Web page, a WML deck, and an ASCII text file...all from the same source XML.
There's only one problem here: most browsers don't come with an XML parser or an XSL processor. The latest versions of Internet Explorer and Netscape Gecko do support XML, but older versions don't, and none of them have an XSL processor. And this brings up an obvious problem: how do you use an XML data source to generate HTML for these older browsers?
The solution is to insert an additional layer between the client and the server, which takes care of processing the XML with XSLT and returning the rendered output to the browser. And that's where PHP comes in - PHP4 supports XML parsing, through its DOM and XML extensions, and includes an XSL processor, through its Sablotron extension.
Sablotron, you say. Sounds scary. What's that?
Sablotron is, according to its creators, "...a fast, compact and portable XML toolkit implementing XSLT, DOM and XPath". Developed by Ginger Alliance (http://www.gingerall.com), the Sablotron engine is a powerful, standards-compliant XSLT processor, with support for most common XSLT constructs.
The latest version of PHP comes with an implementation of this engine, allowing PHP developers to perform XSLT transformations using regular PHP functions. Note, however, that you may need to recompile your PHP binary with the "--with-sablot" parameter to activate XML support (Windows users get a pre-built binary with their distribution.)
Over the course of this article, I'll be demonstrating how you can use PHP's Sablotron extension to perform XSLT transformations of XML data on the server. I'll be assuming you know the basics of PHP, XML, XSLT and XPath; if you don't, you should take a look at the links at the end of the article before proceeding further.