Abbey Workshop

PHP: Transforming XSLT with PHP4

This module covers how to perform an XSLT transformation using PHP. The Sablotron XSLT Processor (http://www.gingerall.com/) has been included with PHP since version 4.1. For XSLT to work, PHP must be configured with the --enable-xslt and the --with-xslt-sablot options set. To see if the modules are included in your PHP executable, type php -m at a command prompt. This gives you a list of the included modules.

This example is pretty simple and is useful for quick XSLT testing from the command line. The details of which are covered in the PHP Cookbook from O'Reilly (see link below). In addition, the book includes an example of transforming XML data read from a URL and stored in memory. You would use this technique if you are using a REST web service like Amazon.com. Here is the source code. The comments pretty much explain what is going on.

transform.php

   1:<?php
   2:
   3:// What are the XML and XSL files?
   4:$xmlFile = "student_directory.xml";
   5:$xslFile = "student_html.xsl";
   6:
   7:// Create a new processor handle
   8:$transformer = @xslt_create() or die("Can't create XSLT handle!");
   9:
  10:// Perform the XSL transformation
  11:$results = @xslt_process($transformer, $xmlFile, $xslFile);
  12:
  13:// Output the transformed XML file
  14:echo $results;
  15:
  16:// Free up the resources
  17:@xslt_free($transformer);
  18:
  19:?>

XML Source

Here is the XML source file used in the tranformation.

student_directory.xml

   1:<student_list>
   2:    <student>
   3:        <name>George Washington</name>
   4:        <major>Politics</major>
   5:        <phone>312-123-4567</phone>
   6:        <email>gw@example.edu</email>
   7:    </student>
   8:    <student>
   9:        <name>Janet Jones</name>
  10:        <major>Undeclared</major>
  11:        <phone>311-122-2233</phone>
  12:        <email>janetj@example.edu</email>
  13:    </student>
  14:    <student>
  15:        <name>Joe Taylor</name>
  16:        <major>Engineering</major>
  17:        <phone>211-111-2333</phone>
  18:        <email>joe@example.edu</email>
  19:    </student>
  20:</student_list>

XSLT Source

Here is the XSLT stylesheet used to perform the tranformation.

student_html.xsl

   1:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   2:<xsl:output method="html"/>
   3:
   4:<xsl:template match="/">
   5:<html>
   6:    <head>
   7:        <title>Student Directory</title>
   8:    </head>
   9:    <body>
  10:    <xsl:apply-templates />
  11:    </body>
  12:</html>
  13:</xsl:template>
  14:
  15:<xsl:template match="student_list">
  16:    <h3>Student Directory for example.edu</h3>
  17:    <xsl:apply-templates />
  18:</xsl:template>
  19:
  20:<xsl:template match="name">
  21:    <p>Name: <xsl:apply-templates /></p>
  22:</xsl:template>
  23:
  24:<xsl:template match="major">
  25:    <p>Major: <xsl:apply-templates /></p>
  26:</xsl:template>
  27:
  28:<xsl:template match="phone">
  29:    <p>Phone: <xsl:apply-templates /></p>
  30:</xsl:template>
  31:
  32:<xsl:template match="email">
  33:    <p>Email: <xsl:apply-templates /></p>
  34:</xsl:template>
  35:
  36:</xsl:stylesheet>