Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button


Listen Print

PHP and the Sablotron Processor

by W.J. Gilmore
03/16/2001

Related Articles:

AxKit: An XML-Delivery Toolkit for Apache

XML-RPC: It Works Both Ways


If you need to learn what XSLT is or how to code it, check out these articles from XML.com:

Entities and XSLT

Extensions to XSLT

What is XSLT?

A few months ago, I ran into a dilemma at work. Allow me to retell the story to the best of my recollection....

"Hey W.J., I need a TOD on that PHP-XML-XSL project for that ASP over in NYC!" said my boss.

"How to get this done ASAP?" I thought while wringing my hands. I stared down at my DKNY jeans and my feet propped up in front of my IBM PC.

Other than suffering from acronym overkill, I had another problem. I needed to build a PHP-powered news application that easily converted XML (Extensible Markup Language) into various formats via XSLT (Extensible Stylesheet Language Transformation) so the client could view the content using either a conventional PC browser or a wireless device (a WAP-enabled cellphone for example). Although at the time I was fairly familiar with XML and XSL syntax, I was having a hard time figuring out exactly how to perform the conversions into the style formats I wanted. That is, until I found one of PHP's newest extensions: the Sablotron processor.

The Sablotron XSLT processor, a product of Ginger Alliance Ltd. out of the Czech Republic, has recently been incorporated into PHP's already vast extension library. Sporting a number of useful functions, it makes basic XSL transformations a snap. In this article, I'll introduce you to this tool, and show you how I ended up building the news application with minimal time and effort. Let's begin with a brief overview of Sablotron and its configuration process as it relates to PHP.

PHP and the Sablotron processor

Sablotron is an expat-based XSLT processor written in C++, which allows for greater portability across platforms. This suits PHP just fine, and it runs great as a PHP extension on both Windows and non-Windows platforms. Take a moment to review the configuration information relevant to your platform:

Non-Windows configurations
To make use of the Sablotron extension with your non-Windows PHP distribution, you'll need to configure PHP with the flag --with-sablot. If you aren't exactly sure how to build extensions into your non-Windows PHP distribution, I strongly suggest checking out Darrel Brogdon's excellent article, Basic Installation of PHP on a Unix System.

Windows configurations
The Win32 binaries available via the PHP site come with the Sablotron extension already built in. Therefore, upon downloading, installing, and configuring the most recent Win32 distribution, open up your php.ini file and uncomment the line:

;extension=php_sablot.dll

Uncommenting the line simply involves removing the semicolon.

Platform-independent configuration procedures

Regardless of the platform, there is another configuration step that you need to perform. Due to the fact that XML and PHP share a common page-structure property -- both use the <?...?> escape tag -- you won't be able to use the short tags format to escape to a PHP document. Instead, you can choose from the three other accepted escape formats PHP allows. The most common way to do this is:

<?php

print "hello world!";

?>

Click here to review all allowed forms of escape syntax.

Incidentally, you could ensure that the short tags style is never used within a PHP document by modifying your php.ini file. If you are interested in doing so, open up php.ini and find the line:

short_open_tag = On

This basically says that it's okay for you to use the short tags when escaping to and from a PHP document. To disallow the short tags style, change this configuration directive to read:

short_open_tag = Off

Okay, your configuration is done. Let's move on to the fun stuff: XSLT!

The Sablotron transformation process

If you've ever worked with file handles in any language, you'll find that performing XSL transformations with the Sablotron processor to be a familiar process. Allow me to break this process down into five generalized steps:

  1. Create a new XSL processor handle.
  2. Read in the respective XML and XSL documents.
  3. Perform the XSL transformation.
  4. Output the XSL transformation to the requesting client.
  5. Free up the allocated XSL resources.

Of course, this generalized process is not specific to the Sablotron processor; this scheme is common in many XSLT processors. Furthermore, this assumes that the intent is to immediately output the content to the device instead of perhaps storing it to a database or performing further functions. However, for sake of introducing the extension, this process flow will work just fine.

Now that you have an idea of the general scheme of events that will take place in the news application, I'll turn my attention toward the project. In the next section, I'll show you how I created my news application using the Sablotron extension.

The news application

The goal of the news application is to create a mechanism for parsing an XML file and transforming it into a format recognizable by either a conventional PC browser or a typical WAP-enabled wireless device. Although the PHP script is relatively short, I've incorporated a few features (device detection, for example) that make it particularly useful. It's also basic enough that you could swipe the code and perform modifications as necessary. Before delving into the code, I'll outline a typical scenario that would involve the script:

  1. User sends request to view the company news.
  2. The Web server hosting the news application recognizes the request, and calls upon the PHP script to perform the necessary actions.
  3. The PHP script sniffs for the browser type, and can thus determine the type of device using that browser. For sake of simplicity, I'll keep this rather abbreviated in terms of browser types.
  4. The script performs the XSL transformation in accordance with the requested transformation format.
  5. The resulting transformation is sent back to the requesting client.

Pages: 1, 2, 3

Next Pagearrow




-->