Convert XML documents using PHP and XSLstylesheets_PHP Tutorial

WBOY
Release: 2016-07-13 17:26:31
Original
692 people have browsed it

PHP is the weapon of choice for many warriors fighting in the field of web development, because it is a very intuitive programming language, has powerful functions, good cross-platform compatibility, and it is free. PHP can be seen in everything from small online stores to large corporate websites. One feature 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 this feature. Example Listing A is a simple order document that we will input into the XSL parser. At the same time, the XSL stylesheet in List B will also be 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 Overview In this example we mainly use three XSL functions in PHP. First, we need to create an instance of the XSL engine, then input all the documents to be input into the XSL engine for processing, and get the returned results. Finally, when we no longer need the XSL engine, we will close it. Create, process, close We are going to create a new XSL process in memory. In order to facilitate the use of this XSL process in other XSL functions, PHP will provide us with a handle to this XSL process instead of an object. The command to create this XSL engine is xslt_create. The function returns a handle, as shown below: $handle = xslt_create(); In order to actually parse the XML document and enable XSLT to process it, you must use the xslt_process function in PHP. This function takes several different parameters. Here we use a very basic method, providing three parameters to xslt_process. The first parameter is the 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 will return the processing result. The following is an example: $return = xslt_process($handle, $xmlfile, $xslfile); The last function we want to use is xslt_free. This function is used to kill the XSL engine instance in memory and free up memory space. It only requires one parameter, which is the handle to the XSL instance in memory. The following is an example: xslt_free($handle); Comprehensive implementation Let us combine the above code snippets to implement the method of PHP processing XML documents through XSL stylesheets. We use List A as our input XML document and List B as our XSL input. Listing C is the complete PHP code for this example: Listing C: order.php "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 some changes in the code. In the XSL stylesheet, we can change some areas, such as addresses, by specifying some parameters. At this point we want to specify that the items on the order should be arranged in descending order of quantity.We use a PHP array to store the names corresponding to our parameters, and then pass the names to the XSL engine through the xslt_process function. The author of this article, Brian Schaffner, is an associate director at Fujitsu Consulting. He provides architecture, design, and development support to Fujitsu's technology consulting firm.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531964.htmlTechArticlePHP is the weapon of choice for many warriors fighting in the field of web development because it is a very intuitive A programming language with powerful functions, good cross-platform compatibility, and it’s free...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template