Presents your
XML E-NEWSLETTER for May 7, 2003
<------------------------------------------->
TRANSFORM XML WITH PHP AND XSL STYLESHEETS
PHP is the weapon of choice for many Web warriors. Because of its
intuitive language, robust functionality, cross-platform compatibility, and
free availability, it's seen very frequently in both small shops and
large enterprises.
One feature that's often overlooked in PHP is its ability to parse XML
documents with XSL stylesheets. Let's look at some of the basics of
setting up an XSL parser in PHP to help you understand how you can put
this feature to use.
THE SAMPLES
LISTING A shows a sample order document. We'll use this document as the
input to our XSL process. The XSL stylesheet in LISTING B will also be
used as input to our XSL process.
LISTING A: order.xml
9900234
-
1234
5.95
100
595.00
Super Widget Clamp
-
6234
22.00
10
220.00
Mighty Foobar Flange
-
9982
2.50
1000
2500.00
Deluxe Doohickie
-
3256
389.00
1
389.00
Muckalucket Bucket
1111
3704.00
07/07/2002
8876
LISTING B: order.xsl
| Account |
SKU |
Description |
Price |
Quantity |
Subtotal |
|
|
|
|
|
|
OVERVIEW
The gist of this example revolves around three XSL functions in PHP.
We're going to start with creating an instance of the XSL engine. Next we'll
send all of our input to the engine for processing and retrieve the
results. Finally, we'll destroy the XSL engine, since we won't need it any
longer.
CREATING, PROCESSING, AND DESTROYING
We're going to create a new XSL processor in memory. Rather than giving
us an object, PHP is going to give us a handle we can use to interact
with the other XSL functions. The command to create a new engine is
xslt_create. The function returns the handle value, like this:
$handle = xslt_create();
In order to actually parse an XML document and process it with XSLT,
you'll need to use PHP's xslt_process function. This function can take a
variety of different parameters.
In the most basic approach, which we'll use here, xslt_process takes
three parameters. The first parameter is the handle to the XSL engine we
created earlier. The second identifies a filename containing the input XML
document, and the third identifies a filename containing the input XSL
file. The function returns the result of the translation process.
Here's a short example:
$return = xslt_process($handle, $xmlfile, $xslfile);
The final function is xslt_free. This function is used to destroy the
instance of the engine that's in memory and free any associated resources.
It takes a single parameter that points to the handle associated with the
engine that's in memory, as shown here:
xslt_free($handle);
PUTTING IT TOGETHER
Let's put together a short example that demonstrates PHP's ability to
process XML documents with XSL stylesheets. We'll use the order shown in
LISTING A as our input document and the stylesheet shown in LISTING B as
our input XSL. LISTING C shows our complete PHP code for this
demonstration.
LISTING C: order.php
"Quantity", "order"=>"descending");
$engine = xslt_create();
$output = xslt_process($engine, $xmlfile, $xslfile, NULL, NULL, $args);
print $output;
xslt_free($engine);
?>
Notice that we've added a slight twist. In our XSL stylesheet, we can
change the field we sort on, as well as the direction, by specifying some
parameters. In this case, we have specified that the items in our order
should be sorted by quantity in descending order. We used a PHP array to
store the name-value pairs for our parameters, and then passed them to the
engine via the xslt_process function.
Brian Schaffner is an associate director for Fujitsu Consulting. He provides
architecture, design, and development support for Fujitsu's Technology
Consulting practice.
----------------------------------------