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:
In order to create and destroy the $xsltproc XSLT processor, you need the following line above xslt_process():
...and the following line below xslt_process():
The complete script, then, is just three lines long: we create a processor, merge the XML and XSL together, then free the processor.
Next chapter: Handling the processed output >>
Previous chapter: Transforming XML using XSLT
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.