Article

Transform your PHP with XSLT

Page: 1 2 3

Test Drive

To try out the new XSLT functionality we've just set up in PHP, I'm going to return to the simple DocBook sample document I used in Get XSL To Do Your Dirty Work. Here's the document in question:

<?xml version="1.0" encoding="UTF-8"?>  
<article>  
 <title>A Sample Article</title>  
 <section>  
   <title>Article Section 1</title>  
   <para>  
   This is the first section of the article. Nothing terribly  
   interesting here, though.  
   </para>  
 </section>  
 <section>  
   <title>Another Section</title>  
   <para>  
   Just so you can see how these things work, here's an  
   itemized list:  
   </para>  
   <itemizedlist>  
     <listitem>  
       <para>The first item in the list</para>  
     </listitem>  
     <listitem>  
       <para>The second item in the list</para>  
     </listitem>  
     <listitem>  
       <para>The third item in the list</para>  
     </listitem>  
   </itemizedlist>  
 </section>  
</article>

Note that I've left out the DOCTYPE declaration at the top of the file. Since the expat XML parser that the Sablotron XSLT processor uses is not a validating parser, the DOCTYPE only serves to slow down the parsing in most cases (as a concession to XML know-it-alls in the audience, there are exceptions).

Here's the XSL stylesheet we developed to display documents in the above format:

<?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"/>  
 
 <xsl:template match="article">  
   <html>  
   <head>  
     <title><xsl:value-of select="title"/></title>  
   </head>  
   <body>  
     <h1><xsl:value-of select="title"/></h1>  
     <xsl:apply-templates select="section"/>  
   </body>  
   </html>  
 </xsl:template>  
 
 <xsl:template match="section">  
   <xsl:apply-templates/>  
   <hr/>  
 </xsl:template>  
 
 <xsl:template match="section/title">  
   <h2><xsl:apply-templates/></h2>  
 </xsl:template>  
 
 <xsl:template match="para">  
   <p><xsl:apply-templates/></p>  
 </xsl:template>  
 
 <xsl:template match="itemizedlist">  
   <ul><xsl:apply-templates/></ul>  
 </xsl:template>  
 
 <xsl:template match="listitem">  
   <li><xsl:apply-templates/></li>  
 </xsl:template>  
</xsl:stylesheet>

Save these on your Web server as docbook.xml and docbook.xsl respectively, then create docbook.php. It will be the job of this script to transform docbook.xml according to the rules in docbook.xsl and to send the HTML output to the user's Web browser.

Here's the code for docbook.php:

<?php  
 // Create an XSLT processor  
 $xsltproc = xslt_create();  
 
 // Perform the transformation  
 $html = xslt_process($xsltproc, 'docbook.xml', 'docbook.xsl');  
 
 // Detect errors  
 if (!$html) die('XSLT processing error: '.xslt_error($xsltproc));  
 
 // Destroy the XSLT processor  
 xslt_free($xsltproc);  
 
 // Output the resulting HTML  
 echo $html;  
?>

This comments in the above code should explain it well. For more information on the functions in use, refer to the PHP manual's section on XSLT processing functions (http://www.php.net/manual/en/ref.xslt.php).

Try this on your server, which has been newly-equipped with XSLT procesing capabilities. You should see the HTML version of the document produced by the Sablotron XSLT engine!

A DocBook document transfored to HTML with XSLT in PHPIt's quite straightforward to modify this script to serve content from your database. Just pull the XML code stored in your database out in the usual fashion, then use xslt_process to transform it into HTML with an XSL stylesheet. The stylesheet can be stored in your database, generated dynamically, or just kept in a file on your server as in the example above.

Explore the rest of the XSLT functions in PHP and you'll be ready to get started on your XSLT-based CMS!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: