Article
Transform your PHP with XSLT
In Get XSL To Do Your Dirty Work, we looked at XSL Transformations (XSLT) and how an XSL processor can be integrated into a traditional Content Management System (CMS). We saw how an XSL processor can take a lot of the grunt work out of coding your average CMS and provide better error-checking for custom tags.
If I convinced you that XSL is something you want to add to your next database-driven Website, you're probably anxious to learn how to integrate an XSL processor with the server-side language of your choice. While many options exist in this arena, this article will show you how to set up and use an XSL processor in PHP, a popular server-side language in the SitePoint community.
NOTE: The XSLT functionality in PHP that is covered in this article was introduced with PHP 4.1.0. PHP saw several experimental revisions of its XSLT module before this release; however, they differed in many ways from the module described here. Please be sure you have PHP 4.1.0 or later before proceeding.
XSLT Module Install for Windows
The first thing you need to do is get PHP up and running on your Windows-based Web server (be it Apache, IIS, or whatever) without XSLT support. If you need help with that, check out my article, Build your own Database Driven Website using PHP & MySQL - Part 1: Installation, which will walk you through the process.
Once you have PHP up and running, the next step is to enable the XSLT module that it comes with. To function, this module makes use of the open-source Sablotron XSLT processor. Sablotron, in turn, relies on the open-source XML parser, Expat. Here's how to install these on your Windows system:
- Download Expat from the project Web site on SourceForge. You'll want the latest version of the expat_win32bin package.
- Install Expat as you would any other program into a convenient directory on your system.
- Locate the
expat.dllfile in theLibssubdirectory of your Expat installation, and copy it to your WindowsSYSTEM32directory (e.g.C:\WINDOWS\SYSTEM32). - Download Sablotron from the Ginger Alliance Web site. The 'Windows binary' release is the one you're after.
- Extract the ZIP file to a convenient place on your system. A
Sablot-versiondirectory will be created for you. - Locate the
sablot.dllfile in thebinsubdirectory of your Sablotron installation, and copy it to your WindowsSYSTEM32directory.
With the required software in place, you can now enable the PHP extension for XSLT support. Open your php.ini file and locate the extension_dir line. Either set it to the extensions directory of your PHP installation, or copy the php_xslt.dll file in that directory to the directory specified by this setting. Then, uncomment (remove the ; from the beginning of) or add the following line to the section under 'Windows Extensions':
extension=php_xslt.dll
With those changes made, save the updated file and close the editor. You've just activated XSLT support in your PHP installation. For this change to affect your Web server, you'll need to restart it (or just restart your computer, which has the same effect, if you're the lazy type).
Once your Web server starts back up, PHP should be fully equipped to process XML data with XSLT. To make sure, you can create a simple PHP script that just calls phpinfo(), then view the results on your server. The page produced should contain the following section:
Now skip past the Linux installation procedure and we'll take the XSLT functions for a test drive.