start page | rating of books | rating of authors | reviews | copyrights

Programming PHPProgramming PHPSearch this book

11.4. Transforming XML with XSLT

Extensible Stylesheet Language Transformations (XSLT) is a language for transforming XML documents into different XML, HTML, or any other format. For example, many web sites offer several formats of their content—HTML, printable HTML, and WML (Wireless Markup Language) are common. The easiest way to present these multiple views of the same information is to maintain one form of the content in XML and use XSLT to produce the HTML, printable HTML, and WML.

PHP's XSLT extension uses the Sablotron C library to provide XSLT support. Sablotron does not ship with PHP—you'll need to download it from http://www.gingerall.com, install it, and then rebuild PHP with the --enable-xslt --with-xslt-sablot option to configure.

PHP's XSLT support is still experimental at the time of writing, and the exact implementation details may change from what is described here. However, this description should give you a good foundation for how to use PHP's XSLT functions, even if the implementation changes in the future.

Three documents are involved in an XSLT transformation: the original XML document, the XSLT document containing transformation rules, and the resulting document. The final document doesn't have to be in XML—a common use of XSLT is to generate HTML from XML. To do an XSLT transformation in PHP, you create an XSLT processor, give it some input to transform, then destroy the processor.

Create a processor with xslt_create( ):

$xslt = xslt_create( );

Process a file with xslt_process( ):

$result = xslt_process(xslt, xml, xsl [, result [, arguments [, parameters ]]]);

The xml and xsl parameters are filenames for the input XML and transformation XSL, respectively. Specify a result filename to store the new document in a file, or omit it to have xslt_process( ) return the new document. The parameters option is an associative array of parameters to your XSL, accessible through xsl:param name="parameter_name".

The arguments option is a roundabout way of working with XML or XSL stored in variables rather than in files. Set xml or xsl to 'arg:/foo', and the value for /foo in the arguments associative array will be used as the text for the XML or XSL document.

Example 11-11 is the XML document we're going to transform. It is in a similar format to many of the news documents you find on the Web.

Example 11-11. XML document

<?xml version="1.0" ?>
  
<news xmlns:news="http://slashdot.org/backslash.dtd">
  <story>
    <title>O'Reilly Publishes Programming PHP</title>
    <url>http://example.org/article.php?id=20020430/458566</url>
    <time>2002-04-30 09:04:23</time>
    <author>Rasmus and some others</author>
  </story>
  
  <story>
    <title>Transforming XML with PHP Simplified</title>
    <url>http://example.org/article.php?id=20020430/458566</url>
    <time>2002-04-30 09:04:23</time>
    <author>k.tatroe</author>
  </story>
</news>

Example 11-12 is the XSL document we'll use to transform the XML document into HTML. Each xsl:template element contains a rule for dealing with part of the input document.

Example 11-12. News XSL transform

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output
  method="html"
  indent="yes"
  encoding="utf-8"
/>
  
<xsl:template match="/news"> 
  <html> 
    <head> 
      <title>Current Stories</title> 
    </head> 
    <body bgcolor="white" >
      <xsl:call-template name="stories"/>
    </body>
  </html>
</xsl:template> 
  
<xsl:template name="stories">
  <xsl:for-each select="story">
    <h1><xsl:value-of select="title" /></h1>
  
    <p>
      <xsl:value-of select="author"/> (<xsl:value-of select="time"/>)<br/>
      <xsl:value-of select="teaser"/>
      [ <a href="{url}">More</a> ]
    </p>
  
    <hr />
  </xsl:for-each>
</xsl:template>
  
</xsl:stylesheet> 

Example 11-13 is the very small amount of code necessary to transform the XML document into an HTML document using the XSL style sheet. We create a processor, run the files through it, and print the result.

Example 11-13. XSL transformation from files

<?php
 $processor = xslt_create( );
 $result = xslt_process($processor, 'news.xml', 'news.xsl');
 if(!$result) echo xslt_error($processor);
 xslt_free($processor);
  
 echo "<pre>$result</pre>";
?>

Example 11-14 contains the same transformation as Example 10-13 but uses XML and XSL values from an array instead of going directly to files. In this example there's not much point in using this technique, as we get the array values from files. But if the XML document or XSL transformation is dynamically generated, fetched from a database, or downloaded over a network connection, it's more convenient to process from a string than from a file.

Example 11-14. XSL transformation from variables

<?php
 $xml = join('', file('news.xml'));
 $xsl = join('', file('news.xsl'));
 $arguments = array('/_xml' => $xml, '/_xsl' => $xsl);
  
 $processor = xslt_create( );
 $result = xslt_process($processor, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
 if(!$result) exho xlst_error($processor);
 xslt_free($processor);
  
 echo "<pre>$result</pre>";
?>

Although it doesn't specifically discuss PHP, Doug Tidwell's XSLT (O'Reilly) provides a detailed guide to the syntax of XSLT stylesheets.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.