Section 19.2. Transforming XML Using XSLT


19.2. Transforming XML Using XSLT

XSLT is an XML-based language that allows you to manipulate XML documents before outputting them. With one XML document, you can make the same content look vastly differentfor example, you could transform it with a WML XSL stylesheet and send it to WAP devices, or parse it with an SQL XSL stylesheet and send it to a database.

Several browsers (most notably Firefox and Internet Explorer) can perform XSL transformation on the client side by downloading an XML document, the XSL stylesheet, and any accompanying CSS files, then combining them all together on your visitor's computer. But someone with an old version of IE, or any other non-XSL-enabled browser, would not get the same experience.

This is where PHP comes in: your visitor types a URL as usual, but it is PHP that loads the XML and the XSL and combines the two together into the output. On the client side, users see no XML or XSL at all, just normal XHTML. Of course, there is nothing stopping that PHP page from analyzing the visitor's user agent and sending content fit for that browser, whether it be HTML 2, XHTML, WAP, or anything else.

19.2.1. An Example XSL Document

Here is an example XSL document designed to work on the employees.xml file from before. Save it in the same directory, as input.xsl:

     <?xml version="1.0" encoding="utf-8"?>     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"             xmlns="http://my.netscape.com/rdf/simple/0.9/">             <xsl:output method="html" indent="no" encoding="utf-8"/>             <xsl:template match="/">                     <html>                     <head>                     <title>XSLT</title>                     </head>                     <body>                     <xsl:for-each select="/employees/employee">                             Job Title: <xsl:value-of select="title"/><br/>                     </xsl:for-each>                     </body>                     </html>             </xsl:template>     </xsl:stylesheet> 

As this is not a book on XSLT (there are enough of those available already!), we will not spend much time analyzing what that does to our XML.

After the long document type header that is the norm with XML-based languages, we come to the line starting "<xsl-template". This matches the root of our XML input, and prints out some basic HTML to give our page a minimum structure. The "<xsl:for-each" line is basically an array iterator, like the foreach construct in PHP. Here, the array is whatever XML elements are found by pattern matching against the "select" attribute of the for-each, which is /employees/employee in the example.

The for-each loop contains a line that prints out the value of the title attribute of each employee. The foreach code is executed once for every matching element it finds in the input XML, so given our employees.xml, it will execute twice.

19.2.2. Adding PHP to the Mix

PHP's XSL support was rewritten for PHP 5, and although you can retrieve the old extension from PECL, it is not recommended.


PHP uses the libxslt library to perform internal XSLT transformations, presenting its functions through an object-oriented interface.

There are two classes you need to know about to use XSLT: DOMDocument, which holds XML data, and XSLTProcessor, which does the transformation. The DOMDocument class is also interesting for more advanced SimpleXML users, as the two extensions can share their XML data.

To perform XSLT transformation, you need two instances of DOMDocument (one for the XML, and one for the XSL) and one instance of XSLTProcessor. You load XML documents into a DOMDocument class by calling its load( ), like this:

     $xml = new DOMDocument;     $xml->load("employees.xml");     $xsl = new DOMDocument;     $xsl->load("input.xsl"); 

Then, to perform the XSLT transformation, you need to use XSLTProcessor's importStyleSheet( ) function to load your XSL, then its transformToXML( ) function to load your XML and transform it. The transformToXML sends back transformed content as its return value.

The full PHP code looks like this:

     $xsl = new DOMDocument( );     $xml = new DOMDocument( );     $xsl->load("input.xsl");     $xml->load("employees.xml");     $xsltproc = new XSLTProcessor( );     $xsltproc->importStylesheet($xsl);     echo $xsltproc->transformToXML($xml); 



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net