Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT > Transforming XML using XSLT Wish List | Report Bug | About Me ]

12.4.1     Adding PHP to the mix

This is NOT the latest copy of this book; click here for the latest version.

We now have a file of well-formed XML and a file of well-formed XSL, but they remain separate. In order to combine the two together to make a final page, you need to mix in the magic ingredient: PHP.

As there are only thirteen XSLT functions in PHP at the time of writing, the learning curve is fairly smooth. The three key functions to learn are xslt_create(), xslt_free(), and xslt_process().

xslt_create() and xslt_free() are used together to create and destroy XSLT processors. As all XSLT processing in PHP is performed by a processor, you will need to call xslt_create() at least once in order to get started. Note that it returns the XSLT processor resource you should use for manipulation in other XSLT functions. For example, xslt_free() takes an XSLT resource as its only parameter, and frees up the memory associated with the provided processor.

This just leaves xslt_process(), which is the core function in PHP XSLT parsing. This function, which can take a maximum of six parameters in total, is where we combine XML and XSLT. The result is transformed XML, the great thing being that it has been transformed into the format you want.

The first three parameters for xslt_process() are the XSLT processor resource to use, the location of the XML file, and the location of the XSL file. These are the only parameters that are required in order to perform the transformation; all the others are optional, and can usually be ignored.

As you know how to create an XSLT processor, and you already have input.xml and input.xsl, your xslt_process() function call is very straightforward:

xslt_process($xsltproc,'/path/to/input.xml','/path/to/input.xsl');

In order to create and destroy the $xsltproc XSLT processor, you need the following line above xslt_process():

$xsltproc=xslt_create();

...and the following line below xslt_process():

xslt_free($xsltproc);

The complete script, then, is just three lines long: we create a processor, merge the XML and XSL together, then free the processor.





<< 12.4 Transforming XML using XSLT   12.4.2 Handling the processed output >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow