PHP is a weapon chosen by many warriors in the field of web development because it is a very intuitive programming language with powerful functions, good cross-platform compatibility, and it is free. PHP's shadow can be seen from small stores on the web to the websites of large companies.
One of the features of PHP that is often overlooked is the ability to work with XSL stylesheets to parse XML. Let's look at how to set up an XSL parser in PHP and how you can use this feature.
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
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
In this example, we mainly use three XSL functions in PHP. First we'll create an instance of the XSL engine, and then we'll get all the documents we want to enter into this XSL engine for processing, and we'll return the results, and finally we'll 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 the 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 the XML document and make the 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 a handle to the XSL engine 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 result of the processing. Here is an example:
$return = Xslt_process ($handle, $xmlfile, $xslfile);
The last function we need to use is xslt_free. This function is used to kill an in-memory XSL engine instance and free up memory space. It requires only one parameter, which is the handle to the XSL instance in memory. Here's an example:
Xslt_free ($handle);
Integrated implementation
Let's combine the various code snippets above to implement PHP's approach to working with XML documents through XSL stylesheets. We use List A as our input XML document, and List B as our XSL input. List C is the full PHP code for this example:
Listing c:order.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 note here is that we made a little change in the code. In XSL stylesheet, by specifying some parameters, we can change some areas, such as addresses. At this point we want to specify that the items on the order should be arranged in a decreasing number of ways. We use an array of PHP to store the names corresponding to our parameters, and then pass the names to the XSL engine through the xslt_process function.
The writer, Brian Schaffner, is the associate director of Fujitsu Consulting. He provides architectural, design and development support to Fujitsu's technology consulting firm.
http://www.bkjia.com/PHPjc/314936.html www.bkjia.com true http://www.bkjia.com/PHPjc/314936.html techarticle PHP is a lot of warriors in the field of web development fighting the use of weapons, because it is a very intuitive programming language, has powerful functions, good cross-platform compatibility, and it is free ...