[ Team LiB ] Previous Section Next Section

XSL: A Brief Discussion

Extensible Stylesheet Language (XSL) is a templating system for XML documents, and with it you can process an XML document for output. With the same XML source, you might apply different XSL documents to format for the Web, PDAs, interactive television, and mobile phones.

Unfortunately, the details of XSL are beyond the scope of this book, but we can briefly examine PHP's support for it.

PHP and XSL

PHP's support for XSL is also currently in flux. The underlying library that PHP 5 now uses is libxslt (http://xmlsoft.org/XSLT/). This is a radical departure from previous versions of PHP, which worked with the Sablotron XSLT processor.

Because work is not yet complete, everything covered in this section is subject to change. Before using XSL in projects, you should visit the PHP manual (http://www.php.net/manual/en/ref.xslt.php) to check the current stability of support for the technology.

Although at the time of writing, XSL support is flagged as experimental and documentation is nonexistent, an easy-to-use and nicely integrated XSLT parser class is already available. Because libxslt is built on the libxml2 library that the DOM and Parser functions already use, PHP's XSL support now works directly with DomDocument objects.

At the time of writing, the libxslt library was not bundled with PHP 5; however, you can download it from http://xmlsoft.org/XSLT. You also might need to compile PHP with XSL support. You should include the argument


--with-xsl

when you run the configure script.

An XSL Document

In Listing 22.7, we apply a simple XSL document to the XML we created in Listing 22.1. It outputs a table for each article, adding formatting and changing the order of two of the siblings.

Listing 22.7 An XSL Document
 1: <?xml version="1.0"?>
 2: <xsl:stylesheet
 3:   version="1.0"
 4:   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 5:
 6: <xsl:output method="html" />
 7: <xsl:template match="banana-news">
 8:   <table border="1">
 9:     <xsl:apply-templates select="newsitem" />
10:   </table>
11: </xsl:template>
12:
13: <xsl:template match="newsitem">
14:   <tr><td>
15:     <i><xsl:value-of select="byline" /></i>
16:     <br />
17:     <xsl:text> writes</xsl:text>
18:     <b><xsl:value-of select="headline" /></b>
19:   </td></tr>
20: </xsl:template>
21: </xsl:stylesheet>

Without getting in too deep with XSL, the purpose of this document should be relatively clear with a close look. Take a look at the first line. An XSL document is also an XML document! The root element


<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

should always take this form. It establishes the XSL namespace and version number.

The <xsl:template> element on line 7 attempts to match the root element. After the match occurs, we establish some basic formatting and with <xsl:apply-templates> on line 9 we attempt to match <newsitem> elements and generate formatted XHTML for each one.

The HTML you see in Listing 22.7 is subject to the same rules as any XML document, which means that failure to close a <tr> or <td> element would cause a parser to generate an error message. The <xsl:value-of> tags (lines 15 and 18) are substituted by the value of the elements stipulated in their select attribute (<byline> and <headline>). Notice that we have switched the positions of byline and headline elements we are matching. XSL gives you control over the structure of data in output as well as its format.

Applying XSL to XML with PHP

Now that we have an XSL document, we can use it to transform our XML. In fact, to do this we only need to use a few functions. Listing 22.8 introduces them.

Listing 22.8 Using XSL to Transform an XML Document
 1: <?php
 2: $xslt = new xsltprocessor();
 3:
 4: $xml_doc = new DomDocument();
 5: $xml_doc->loadXML( file_get_contents("./listing22.1.xml") );
 6:
 7: $xsl_doc = new DomDocument();
 8: $xsl_doc->loadXML( file_get_contents("./listing22.7.xsl") );
 9:
10: $xslt->importStylesheet( $xsl_doc );
11: print $xslt->transformToXml( $xml_doc );
12: ?>

In Listing 22.8 we use the new XsltProcessor class to work with an XSL document and an XML document to produce formatted text. We initialize DomDocument objects to store our XSL and XML on lines 4 and 7. We then use the loadXML() method to acquire XML data on lines 5 and 8.

We now have an XsltProcessor object and two primed DomDocument objects. On line 10 we call XsltProcessor::importStylesheet(), passing it the DomDocument object containing our XSL. Finally, on line 11 we call transform_to_xml(), passing the method the DomDocument containing the XML to be transformed. The transformToXml() method returns the results of the transformation as a string we print.

    [ Team LiB ] Previous Section Next Section