Use PHP and XSLstylesheets to convert XML documents

Source: Internet
Author: User
Tags xsl file xsl stylesheet
Using PHP and XSLstylesheets to convert XML documents PHP is a weapon used by many Web developers because it is a very intuitive programming language with powerful functions, good cross-platform compatibility, and it is free of charge. PHP shadows can be seen from small online stores to websites of large enterprises.


One of the features of PHP is often overlooked, that is, the ability to work with XSL stylesheets to parse XML. Next let's take a look at how to set up an XSL parser in PHP and how to use this function.


Example
List A is A simple order document. we will enter this document into the XSL parser. In addition, the XSL stylesheet in list B will also be input 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 need to create an XSL Engine instance, then input all the documents to be input into this XSL engine for processing, and get the returned results. finally, disable the XSL engine when it is no longer needed.


Create, process, and close
We will create an XSL process in the memory. To facilitate the use of this XSL process in other XSL functions, PHP will provide us with a handle to this XSL process, rather than an object. The command to create this XSL engine is cmdt_create. The function returns a handle as follows:

$ Handle = effect_create ();

To parse XML documents and enable XSLT processing, you must use the xslt_process function in PHP. This function requires several different parameters.

Here we use a very basic method to provide three parameters for effect_process. The first parameter is the handle of the XSL engine we created earlier. The second parameter is the name of the input XML document. The third parameter is the file name of the input XSL file. This function returns the processing result. The following is an example:

$ Return = effect_process ($ handle, $ xmlfile, $ Export file );

The final function we will use is effect_free. This function is used to kill the XSL Engine instance in the memory and release the memory space. It only needs one parameter, that is, the handle of the XSL instance in the memory. The following is an example:

Effect_free ($ handle );

Comprehensive Implementation

The following describes how to use XSL stylesheets to process XML documents in PHP based on the preceding code snippets. We use list A as our input XML document, and list B as our XSL input. List C is the complete PHP code for this example:

Listing C: order. php

$ Xmlfile = "order. xml ";
$ Export file = "order. xsl ";
$ Args = array ("column" => "Quantity", "order" => "descending ");
$ Engine = effect_create ();
$ Output = effect_process ($ engine, $ xmlfile, $ Export file, NULL, NULL, $ args );
Print $ output;
Effect_free ($ engine );
?>

Note that we have made some changes in the code. In XSL stylesheet, by specifying some parameters, we can change some regions, such as addresses. In this case, we need to specify that the items on the order should be arranged in descending order of quantity. We use the PHP array to store the parameter corresponding to our name, and then pass the name to the XSL engine through the effect_process function.


Brian Schaffner is the deputy director of Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's technical consulting companies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.