#native_company# #native_desc#
#native_cta#

Transforming XML with XSL using Sablotron Page 4

By Justin Grant
on October 24, 2000

Let’s see a screenshot of what the result of this transform would look like.
The image below is the result of reading a “live” XML document from
the web instead of the sample shown above, after all having up- to-date news
can be fun while testing out the XSL transforms.

Looks kinda cool, doesn’t it ?!, there’s so much more you can do with XML/XSLT
but I’ll leave that for you to research. We could even write an XSL transformation
that generates documents viewable by PDA’s and Cellular phones.
Let me focus on the code used to do this transform (after all this article
is supposed to be about PHP).
I wrote a class to wrap the Sablotron functions in keeping with good OOP practises.
Here is the script that uses the class. By looking at the code it becomes obvious
why using PHP Objects is a good idea and can simplify web development.

<?php

// 30.07.2000 by Justin Grant

include("class.XSLTransformer.php");

$xml="http://www.slashdot.org/slashdot.xml";

$xsl="slashdot.xsl";

$transform = new XSLTransformer();

if(
$transform->setXsl($xsl)) {

           if(
$transform->setXml($xml)) {

              
$transform->transform();

              if (
$transform->getError() == 0) {

                 echo 
$transform->getOutput();

              } else {

                 echo 
"<p>Error transforming ",$xml,".</p>n";

              }

           } else {

              echo 
"<p>",$xml,": ",$transform->getError(),"</p>n";

           }

$transform->destroy();

}

?>



In the script, the XSLTransformer class file is included.
I define the URL to the XML document and file path to the XSL document.
I then create an instance of the transformer class.
If I can successfully load the XSL document then I try and load the XML document.
If these documents are successfully loaded then the transform is performed and
the result is written out to the browser.
If loading one or both of these documents is unsuccessful then I just write
out the error message property of the object.
Finally in good OOP style I destroy the object because I’m done with it. It’s
that simple !