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