![]() Join Up! 96817 members and counting! |
|
|||
Using XML: A PHP Developer's Primer, Part 3
Adam Delves
XSLT in PHP
Like XML, PHP 4 provides
the XSLT
extension, while PHP 5
provides the W3C compliment XSL
extension. We will focus here on
PHP 5's implementation of the XSL extension. Before using XSL, it must
be enabled and the required libraries must also be installed on the
host operating system.
Installation
Windows To check whether or not XSL is
already enabled on a server,
create a file which calls the phpinfo()
function and check
for the XSL section.
On Windows, XSL support requires two dll files.
To enable XSL support, add or
uncomment the following line in the php.ini file:
extension=php_xsl.dll Unix On Unix platforms, the XSL
extension needs to be enabled at compile
time and requires the libxslt libraries from xmlsoft
to be
installed. After obtaining the source code from xmlsoft, extract it and
follow the familiar configure, make, make install, routine to
install them:
# tar xvf libxslt-1.1.2.tar.bz2 -–bzip2Finally, we need to recompile PHP
enabling the XSL extension:
# cd php-5.1.2Transforming XML with PHP
Transforming an XML document
generally requires the following steps:
PHP
- book_summary.php:<?php The transformToXml method transforms the XML using the imported stylesheet, returning the string representation of the transformed XML. Similarly, the transformToDoc function can be used if you require a DOMDocument representation of the transformed XML. Using XSLT to transform XML makes the code cleaner and allows for clear separation between processing logic and display logic. It also serves to provide a robust way of describing the transformation in a multi-platform, multi-language environment. As we will discover later, we can apply the same XSL stylesheet to the XML using Javascript too.
Passing Variables to an XSL Stylesheet
One of the most powerful features of XSL is its ability to receive variables from external entities which can be used anywhere in the stylesheet. In XSL, an external variable is declared using the xsl:param element as follows:
<xsl:param
name="select-isbn"
/>The current XSL stylesheet lists all books with details accompanying each one. By adding an external variable and making a slight change to the template we can display information on only one specific book, based on a variable passed in the query string containing the ISBN number. A second external variable called script-path will also be added, which contains the URI of the current script. We need only change the root template. Notice how we now use xsl:for-each to iterate the book list and display the detailed book information at the bottom using the book template: XSL:
The variable's values can be passed to the XSL stylesheet using the setParameter function. The next time an XML document is transformed, these variables will be available inside the stylesheet: $processor->setParameter('', 'script-path',
$_SERVER['PHP_SELF']);processor->setParameter('',
'select-isbn', htmlentities(@$_GET['isbn']));The first function parameter sets the namespace URI for the parameter. This can be left as a zero length string when using the default XSL namespace. XSLT in Javascript
At the time of writing only Internet Explorer and Firefox have support
for XSL transformations. Where possible we should take advantage of
this and allow the transformations to be carried out on the client side. This
not only saves processing time on the server; the XML which is to be
transformed is often smaller in size than its HTML counterpart meaning
bandwidth savings too. Both Firefox and Internet Explorer have quite
different implementations of XSLT.
Firefox
The Firefox XSLT implementation is very similar to PHP's, as, it too is
W3C compliment. In Javascript, we must code more defensively, as we
will be acquiring the XML to be transformed and the XML stylesheet via
an HTTP connection.
The first step is to load the XSL stylesheet into a DOMDocument. This
is achieved with the createDocument function:
var oXsl = document.implementation.createDocument('', '',
null);Notice how we deliberately set async to false here. If you intend to
load the stylesheet while the page is loading, it is best to use a
synchronous request. If you wish to use an asynchronous request, the
DOMDocument fires the onload event handler once the XML document has
loaded completely.
Before continuing, it is wise to check that the XSL stylesheet has been
loaded properly. We can do this by testing the root element of the XML document. If an error occurred while loading the XML document, this
will be replaced with a <parsererror> tag:
if ((!oXsl .documentElement)
|| (oXsl
.documentElement.tagName == 'parsererror')) {Once we have confirmed that the stylesheet has been loaded
correctly, we can import it as a stylesheet and transform it in exactly
the same way as PHP:
var oXslt = new XSLTProcessor();
|