January 01, 2002
The Sablotron XSL ProcessorW.J. Gilmore
The way an Internet developer embraces a new technology is similar to the act of falling in love. If the introduction and initial courting period go well, a lasting relationship is formed. Many days and nights are spent together, and the developer will brag to colleagues about all of the things he's doing with his new love. However, as human nature would have it, developers are often the unfaithful type, and are commonly found carousing with emerging, sexier technologies. In fact, I've been having an ongoing affair with that tramp HTML (everybody loves her), and although she'll always hold a special place in my heart, I feel it's time to move on. So I've been seeing quite a bit of the eXtensible Markup Language (XML) on the side.
Seriously though, there are quite a few reasons why developers should begin learning more about what XML has to offer. One reason is the ease in which XML can be used to communicate information across not only platforms, but a multitude of devices as well. Through the use of an eXtensible Style Sheet (XSL) processor, a single XML document can be parsed and converted into a format recognizable by the requesting client's device. The implications of doing so should be obvious, not only for the internal interests of your organization, but for your clients as well.
Quite interestingly, PHP (another technology close to my heart) has recently incorporated an XSL processing extension into its already exceedingly large array of functionality. Developed by the Czech Republic-based Ginger Alliance, the Sablotron XSL processor is certainly a welcome feature for the many PHP developers who've been eagerly awaiting further additions to PHP's useful XML-parsing capabilities. In this article, I'll introduce you to some of Sablotron's basic functionality, and give you a few examples that should provide the foundation of knowledge you'll need to get jumpstarted using this cool new extension.
To perform the XSL transformation, you'll need three files: the XML file to be transformed, the XSL file that sets the rules for the transformation, and the PHP script that calls the functions made available by the Sablotron processor. If you don't understand what the first two files are or what they do, I suggest reading Bonnie SooHoo's excellent XML/XSL primer. Of course, coding the PHP script is the subject of this article. With that said, let me begin by generalizing code found within this script. The script's functions are:
Let's begin by taking care of the first step in this process. A processor handle is created by executing the function
$proc_handle = @xslt_create() or die("Can't create XSLT handle!");
Simple enough, right?
The next step involves reading in the XML and XSL documents. For sake of illustration, here's each document, respectively:
Reading in each document actually doesn't involve any special functionality found within the Sablotron processor. This is accomplished using two of PHP's filehandling functions:
<?
$xml_file = "recipes.xml";
$xsl_file = "pcrecipes.xsl";
// Open each file and assign it to a filehandle.
$xml_handle = fopen($xml_file, "r") or die("Can't open XML file!");
$xsl_handle = fopen($xsl_file, "r") or die("Can't open XSL file!");
// Read in the file contents.
$xml_content = fread($xml_handle, filesize($xml_file));
$xsl_content = fread($xsl_handle, filesize($xsl_file));
?>
Next comes the fun part; the XSLT processing. I think you'll be pleasantly surprised about how easy this is. A simple call to xslt_process() is all it takes:
xslt_process($xsl_content, $xml_content, $xsl_tfmtion);As you can see, three parameters are required. The first two, $xml_content and $xsl_content, are the XSL and XML contents, respectively. The third file is the variable that'll contain the resulting transformation. Chances are you'll then want to output the transformation to the requesting client browser:
echo $xsl_tfmtion;Okay, we're almost there. After processing is complete, you should free up the processor resources. This is accomplished with the function xslt_free().
<? $processor = xslt_create(); // do the parsing xslt_free($processor); ?>Putting it all together, a simple transformation script might look like the following:
<?
$xml_file = "recipes.xml";
// XSL for PC browser
$xsl_file = "pcrecipes.xsl";
$proc_handle = @xslt_create() or die("Can't create XSLT handle!");
// Open each file and assign it to a filehandle.
$xml_handle = fopen($xml_file, "r") or die("Can't open XML file!");
$xsl_handle = fopen($xsl_file, "r") or die("Can't open XSL file!");
// Read in the file contents.
$xml_content = fread($xml_handle, filesize($xml_file));
$xsl_content = fread($xsl_handle, filesize($xsl_file));
// Perform the transformation. xslt_process($xsl_content, $xml_content, $xsl_tfmtion);
// Output the transformed document.
echo $xsl_tfmtion;
// Free up the processor resources.
xslt_free($processor);
?>
Implementing Multiple XSLTsAlthough I initially alluded to the fact that XML is useful because it can be easily transformed into different formats according to the requirements of the requesting device, for sake of illustration I've only shown how XML can be transformed into HTML. Chances are you'll want to set up a number of style sheets capable of handling transformations geared towards such devices as cell phones, pagers, PDAs, in addition to the regular old PC browser. But how would you know which style sheet to use? Thankfully, PHP handles this problem for t us, providing valuable client information within the variable$HTTP_USER_AGENT. With this in mind, you could modify the above code to dynamically input a different XSLT according to the information found within this variable. For sake of illustration, assume that you want to provide information to both the typical PC browser and a WAP-enabled phone:
<?
// A few phone browsers
$wap_browsers = array("AUR","Eric","Nok","UP.B");
$xml_file = "recipes.xml";
$agent = substr(trim($HTTP_USER_AGENT),0,4);
if (in_array($agent, $wap_browsers)) :
$xsl_file = "phonerecipes.xsl";
else:
$xsl_file = "pcrecipes.xsl";
endif;
. . .
?>
Of course, the
Conclusion
As of the time of this writing, the Sablotron extension was so new to the PHP distribution that documentation wasn't even yet available. However, several of the discussion groups are already beginning to buzz about the prospects of this interesting new feature.
If you end up doing anything really cool with this extension, please let me know about it! I'd love to relay it to the WebReview.com readers.
Jason has been an Internet developer since 1995, and is the author of the upcoming Programmer's Introduction to PHP from APress.
|
|
||||||||||||||||||||||||||||||
|
|
![]() |
||||
![]() |
|
|||
![]() |
||||
![]() |
||||
![]() |
|
|||
![]() |
|
|||