Ray Hunter on 2002 May 08
Part 2: The Power of XSLT
This is the second article in The Power of Three: XML, XSLT and PHP series. I am going to help you set up Sablotron on PHP so that you are able to parse XML files with XSLT in PHP with the use of Sablotron (A PHP extension for parsing XSLT files). I will walk you through the set up Sablotron and then configuring it with PHP. This allows you to use XML and XSLT together with PHP. Our goal is to be able to open XML files in PHP and transform them with XSLT
So, what is XSLT?
XSLT is a language for transforming XML documents. In other words, a language used to present the data contained in XML documents. Throughout the article you will read out transformation, let me clarify this word and say it means to display or present something. For example, HTML is a markup language that can also "present" data in the way you want. XML cannot display the data in a specific way - you have to use XSL to "transform" it or "present" it. That where we get XSLT as XSL transformation. Just remember that this is a W3C standard and you can research more about XSLT at http://www.w3c.org/xslt . Don't worry if you do not fully understand XML, XSL and XSLT. You will get it as we work through the examples.
Why do we need XSLT?
This is a great question! What do you think would be the use for XSLT? If we already have XSL, then why do we need XSLT? Let me explain why...XSLT presents or formats XML data in to other formats. That is it. Remember from my first article, Part 1: THe Power of XML, the other forms of XML documents can be spreadsheets or mathematical equations. Some other types of documents can be HTML pages. I use XSLT to create many of my HTML pages and PDF files. Some database systems use XSLT to present XML data to clients.
So do we have to use XSLT? No, you do not have to, however, XSLT is one of the powers of presentation that you definitely need on the internet.
XSLT support in PHP
How do we use XSLT in PHP? That is easy'we have to use Sablotron in UNIX style systems and the php_xslt.dll on Windows systems.
NOTE: Before you configure make sure that you are not on a production site. If you mess up on this it may take you some time to get this running. I always use a test box for new stuff.
What is Sablotron? "Sablotron is a fast, compact and portable XML toolkit implementing XSLT, DOM and XPath." - Sablotron Website
So you want XSLT with PHP on Windows. First I suggest doing the following, which is probably the easiest way to get the PHP extensions for PHP on windows:
1. Download both of the windows installation files: the installer and the zip file from PHP.net.
2. Run the installer.
3. After running the installer you can unzip the PHP zip file in the PHP directory that was created with the installer (most likely C:php).
4. When you do that you will be copying over same files, which is okay. You will have new directories that have extensions and additional "dlls" that you need.
5. For simplicity I suggest copying all dlls to your system32 directory'C:Windowssystem32 or C:winntsystem32.
6. Also copy all dlls to the directory where the php.exe file is located.
7. Next copy the php.ini-recommended to you C:winnt or C:windows directory and rename it to php.ini. (This is the configuration file for PHP.)
8. Edit your php.ini file and search for the DLL section. Lines are commented out with the ";". You can uncomment the DLLs that you want.
Now let us learn how to install Sablotron on UNIX type systems:
NOTE: I'll be upfront on this; Sablotron is a pain in the ass to get to run under Unix/Linux systems. You can run into many configuration errors.
First, let's get iconv (libiconv), which is a character set conversion GNU utility. Sablotron will use this. Trust me, you really do not want me to go into much detail for reason why we need this. We just do. Download from ftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.7.tar.gz .
Command line steps:
tar -xvzf libiconv-1.7.tar.gz
mv libiconv-1.7/ /usr/local/iconv
cd /usr/local/iconv
./configure
--prefix=/usr/local/iconv
--libdir=/system/lib/dir
--includedir=/system/lib/dir
lib directory is where your system keeps most of the library files. On RedHat it is located in /usr/lib and the include directory is located in the /usr/include.
make
make install
We need to get the latest version of Sablotron. You can download the tar file from their website and save it to your system at http://www.gingerall.com/charlie/ga/xml/p_sab.xml .
Then follow the steps below.
Command line steps:
tar -xvzf Sablot-0.90.tar.gz
mv Sablot-0.90/ /usr/local/sablot/
cd /usr/local/sablot
./configure
--prefix=/usr/local/sablot
gmake
gmake install (as super user)
Now you have Sablotron on your system. Here comes the hard part, getting PHP to use Sablotron.
Now we are ready to configure PHP with Sablotron and XML. I will give instructions for a new install and a reconfiguration.
For a new install, just run configure as thus:
tar -xvzf php-4.2.0.tar.gz
mv php-4.2.0/ /usr/local/php
cd /usr/local/php
./configure
--prefix=/usr/local/php
--with-xml
--enable-xslt
--with-xslt-sablot=/sablot/install/dir
--with-iconv
make
make install
Before you reinstall you should save a copy of the configuration that you did when you installed the last time. What I do is create a file with the phpinfo function and display that file in my browser. I then copy the configuration section to a text file for later use.
For a reinstall, here are the steps:
cd /php/install/dir
rm -f config.cache (if you have version less than 4.2)
./configure
--prefix=/usr/local/php
--with-xml
--enable-xslt
--with-xslt-sablot=/sablot/install/dir
--with-iconv
make
make install
Remember that many times even the advanced users have problems with this type of configuration. You probably will not get this going on the first try.
After we get this all made then we are ready to move on'
After you get this going, sit back get a cold one and say, "Damn, I am good!" You deserve it'
XSLT file example
Great job on getting Sablotron compiled with PHP! Now we are ready to create our first XSLT file for our XML file. First, let's look at our XML file from last week.
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<person>
<name>
<firstname>John</firstname>
<middlename>Michael</middlename>
<lastname>Stevens</lastname>
</name>
<email>jstevens@company.com</email>
<phone>
<home>999 999-9999</home>
<cellular>888 888-8888</cellular>
</phone>
<department>Software Engineering</department>
</person>
Now let's create a XSLT file that will transform the data in this XML file into another format such as HTML. Since we are using PHP and we are mainly concerned with web-based programming, we are going to create an XSL Transformation to HTML. That makes sense for most applications, don't you agree?
First let start with the basic syntax:
We need to declare the xml file
Next we need to define the style sheet type
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Here we are declaring the xml format and then defining the type of style sheet that we will be using. It is important that you have these two lines in this file. Now that you have this set up, let's start working on the transformation part.
Now we need to work on the XSLT stuff. Remember that this is only an introduction to XML and XSLT on PHP. I am not trying to give an in-depth discussion on XML and XSLT. However, I would like to give some basics on creating a XSLT file.
Okay, now we need to tell XPath where to start. How do we do that? Easy, with more tags!
This is what you want to do. Specify the start location of the "root node". "Root Node" is the starting tag of the xml file. In our example "person" is the "root node".
Here is the tag that we need:
<xsl:template match="/person">
So what do we have so far in our XSLT file? Let's take a quick look'
XSLT file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/person">
</xsl:template>
</xsl:stylesheet>
Always remember that we need to close the tags that we created. There are many ways to create this file, however, I will only show you one, which is probably the easiest to understand because it has more HTML in it than anything. So here we go!
XSLT file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/person">
<html>
<head>
<title>XML and XSLT Example</title>
</head>
<body>
<table>
<tr>
<td>Personal Information:</td>
</tr>
<tr>
<td><xsl:value-of select="name/firstname" /></td>
<td><xsl:value-of select="name/middlename" /></td>
<td><xsl:value-of select="name/lastname" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><xsl:value-of select="email" /></td>
</tr>
<tr>
<td><xsl:value-of select="department" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
NOTE: It is important to remember that this is an XML file type and all tags need to be closed. You will get errors for all unclosed tags.
Congrats on the good work! Now you have a XML and XSLT file to work with. We are going to stop here and let you practice with the XML and XSLT files before we move into using them with PHP.
Look out for my next article in this series: The power of PHP where I will bring all this together and show you how to use XML, XSLT and PHP together.
It's the Power of Three...
Ray Hunter
rhunter@hunterhysteria.com
