XSL and XSLT Page 41
March 22, 2002
XSL is the XML-based language for describing stylesheets. You've
probably used Cascading Style Sheets (CSS) when working with
static HTML. CSS is used to handle the presentation of an HTML
document, and HTML is used for layout of the page. Within the
XML world, XSL provides the instructions to an XSLT processor,
describing how to handle XML data. So XSL performs a similar
function as CSS.
An XSL document is well-formed XML and conforms with the XSL
namespace. Although we've already looked at what makes an XML
document well-formed, we haven't looked at namespaces. An XML
namespace is a set of predefined elements. By creating a
standardized list of elements, all XSLT processors will know,
for example, that
<xsl:comment></xsl:comment&
;amp;gt; will create a comment in the target document, like
<!-- --> in HTML. All you need to know is
to include namespace declaration,
xmlns:xsl="http://www.w3.org/1999/XSL/Transform
", in the <xsl:stylesheet>
element. You can find more information about namespaces online
at: http://www.w3.org/TR/REC-xml-names/.
The different parts of XSL are described using the same naming
conventions as XML, so within XSL the building blocks are called
elements and attributes.
In this section of the chapter we will be using a tiny part of
the XSL recommendation. XSL is a large subject and there are
whole books written about it. We will be using enough XSL to
render an XML document as an HTML table. For more information on
XSL, see XSLT Programmer's Reference 2nd Edition from Wrox (ISBN
1-861005-06-7).
Sablotron
To use XSL with PHP you will need to use Sablotron. Sablotron,
also known as Sablot, is a PHP extension that provides support
for XSL, XSLT, and XPath, and is maintained by the Ginger
Alliance (http://www.gingerall.com/). Sablotron requires the
Expat parser to be already installed. If you've gotten this far
through the chapter and have played with the SAX examples, then
you've got Expat installed.
Installation and Verifying XSL
If Sablotron appeared in the phpinfo.php earlier, then it is
already installed. If not, you can download the libraries from
the Ginger Alliance (http://www.gingerall.com/).
UNIX Installation
After downloading the rpms take a look at the README file that
is listed with the rpms. The installation instructions are in
the README file. Install the rpm by typing:
rpm -i sablotron-0.6x-x.i386.rpm
or
rpm -i sablotron-devel-0.6x-x.i386.rpm
After installing Sablotron, you can re-compile PHP with the XSLT
extension, add a –with-sablot string to your configuration
string, re-compile, and install PHP. If you are having problems
refer to the annotated manual on the PHP.net site:
http://www.php.net/manual/en/ref.xslt.php.
Windows Installation
The download of PHP 4.0.6 should contain all the appropriate
dlls to install Sablotron. They are in the PHP\dlls\ directory
of the distribution.
To install Sablotron with PHP and Apache:
-
Stop Apache if it is running.
-
Extract the following dlls into the C:\Windows\System\ folder if
you are running Windows98 or C:\WINNT\System32\ if you are
running WindowsNT/2000:
- expat.dll
- sablot.dll
- xmlparser.dll
- xmltok.dll
-
Open your php.ini file and remove the comment from the line
;extension=php_sablot.dll. Save the php.ini file.
-
Restart Apache.
-
Run the code.
The Windows installer for IIS automatically places these files
in their appropriate places.
XSL Example Code
For this example we need to have three files in the
same directory:
-
travel.xml
-
travel.xsl
-
xslt_travel.php
The RAX Model Page 40
Professional PHP4 Programming
XSL and XSLT Page 42
|