PHP is the weapon of choice for many warriors in the Web development arena because it is a very intuitive programming language with powerful functions, good cross-platform compatibility, and free. PHP's shadow can be seen from small shops on the internet to websites of large enterprises.
One of the features of PHP that is often overlooked is the ability to work with XSL stylesheets to parse XML. Let's take a look at how to set up an XSL parser in PHP and how you can use that functionality.
Example
List A is a simple order document and we will enter this document into the XSL parser. Also, the XSL stylesheet in List B is entered into the XSL parser.
Listing A:order.xml
<?xml version= "1.0"?>
<Order>
<Account>9900234</Account>
<item id= "1" >
<SKU>1234</SKU>
<PricePer>5.95</PricePer>
<Quantity>100</Quantity>
<Subtotal>595.00</Subtotal>
<description>super Widget clamp</description>
</Item>
<item id= "2" >
<SKU>6234</SKU>
<PricePer>22.00</PricePer>
<Quantity>10</Quantity>
<Subtotal>220.00</Subtotal>
<description>mighty Foobar flange</description>
</Item>
<item id= "3" >
<SKU>9982</SKU>
<PricePer>2.50</PricePer>
<Quantity>1000</Quantity>
<Subtotal>2500.00</Subtotal>
<description>deluxe doohickie</description>
</Item>
<item id= "4" >
<SKU>3256</SKU>
<PricePer>389.00</PricePer>
<Quantity>1</Quantity>
<Subtotal>389.00</Subtotal>
<description>muckalucket bucket</description>
</Item>
<NumberItems>1111</NumberItems>
<Total>3704.00</Total>
<OrderDate>07/07/2002</OrderDate>
<OrderNumber>8876</OrderNumber>
</Order>
Listing b:order.xsl
<?xml version= "1.0"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:param name= "column" select= "' SKU '"/>
<xsl:param name= "Order" select= "' Ascending '"/>
<xsl:template match= "/" >
<body>
<xsl:apply-templates select= "Order" >
<xsl:with-param name= "SortColumn" select= "$column"/>
<xsl:with-param name= "SortOrder" select= "$order"/>
</xsl:apply-templates>
</body>
</xsl:template>
<xsl:template match= "Order" >
<xsl:param name= "SortColumn"/>
<xsl:param name= "SortOrder"/>
<table border= "1" >
<tr>
<th>Account</th>
<th>SKU</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<xsl:apply-templates select= "Item" >
<xsl:sort select= "*[name () = $sortcolumn]" order= "{$sortorder}"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match= "Item"
<tr>
<td><xsl:value-of select= ". /account "/></td>
<td><xsl:value-of select=" SKU "/></td
<td><xsl:value-of select= "Description"/></TD>
<td><xsl:value-of select= "Priceper"/></TD>
<td><xsl:value-of select= "Quantity"/></TD>
< td><xsl:value-of select= "Subtotal"/></td>
</tr>
</xsl: template>
</xsl:stylesheet>
Overview
In this example, we mainly use three XSL functions in PHP. First we'll create an instance of an XSL engine and then process all the documents to be entered into the XSL engine and get the results back, and then close it when we don't need the XSL engine anymore.
Create, process, close
We're going to create a new XSL process in memory. To facilitate the use of this XSL process in other XSL functions, PHP provides us with a handle to this XSL process, not an object. The command to build this XSL engine is xslt_create. The function returns a handle, as follows:
$handle = Xslt_create ();
To really parse XML documents and make XSLT work, you have to use the Xslt_process function in PHP. This function needs to get several different parameters.
Here we use a very basic method that provides three parameters for xslt_process. The first parameter is the handle to the XSL engine that we created earlier. The second parameter is the file name of the input XML document. The third parameter is the file name of the input XSL file. This function returns the processing result. Here is an example:
$return = Xslt_process ($handle, $xmlfile, $xslfile);
The last function we're going to use is xslt_free. This function is used to kill an instance of an XSL engine in memory and free up memory space. It requires only one parameter, which is the handle of this XSL instance in memory. Here's an example:
Xslt_free ($handle);
Integrated implementation
Let's combine the code snippets above to implement the way PHP processes XML documents through XSL stylesheets. We use List A as our input XML document, listing B as our XSL input. List C is the complete PHP code for this example:
Listing c:order.php
<?php
$xmlfile = "Order.xml";
$xslfile = "order.xsl";
$args = Array ("column" => "Quantity", "Order" => "descending");
$engine = Xslt_create ();
$output = Xslt_process ($engine, $xmlfile, $xslfile, NULL, NULL, $args);
Print $output;
Xslt_free ($engine);
?>
One thing to be aware of here is that we made a little change in the code. In XSL stylesheet, we can change some areas, such as addresses, by specifying some parameters. At this point we want to specify that items on the order should be sorted in descending quantity. We use an array of PHP to store the names corresponding to our parameters, and then pass the name to the XSL engine via the xslt_process function.
Brian Schaffner, the author of this article, is the Deputy director of Fujitsu consulting firm. He provides architectural, design and development support to Fujitsu's technical consulting companies.