In my last article I covered
the use of the expat functions available to PHP for parsing XML documents using
the SAX method. The sample code was a class that read the XML document and generated
the HTML to present the data in a nice table. This example was far from perfect
due to the fact that the presentation of the data was not separate from the
class code that had to parse the XML document. This meant that a slight modification
to the HTML generated by the class could break the actual parsing functionality
of the class.
This is not the ideal way of doing things in a production environment, hence
the purpose of this article in explaining how to parse XML documents using XSLT
(eXtensible Stylesheet Language Transformations). XSLT is a language for transforming
XML documents into other XML documents or HTML documents. I will not discuss
XML, XSL or XSLT syntax in this article, for a good introduction to these please
refer to the links at the end of this article.The focus of this article is on
how to transform XML documents with XSLT using the Sablotron XSLT processor.
Sablotron is the result of a project to develop a fast, reliable, compact and
portable XSLT processor conforming to the W3C specification. It currently supports
Linux, Windows NT and, beginning with version 0.42, Solaris. It requires the
expat XML Parser. In addition to these pieces of software the PHP4 sablotron
extension wrapper is required to build an extension module that you can load
with the PHP4 parser to give you access to the Sablotron API. This may sound
a bit complex but it's really not and the result is well worth the power that
these functions will provide you with. Some links to instructions on how to
do all this are provided at the end of the article.
To demonstrate the use of these functions I have written an XSLT class that
takes XML and XSL wether it be in the format of a file, a URL or a String and
performs the transform. I use this class to to read an XML document from the
web (the Slashdot news!) and transform it into an HTML page using an XSLT document
on the local file system.
I'll do my best to explain how I've done this and welcome questions in the followup
section, so here goes nothing ! ...