Here's some PHP code that uses the Sablotron PHP extensions to transform XML with a stylesheet. The parameters to the constructor can be URLs, filenames, or one of each. The code is dying on the xslt_process command as elaborated upon in the code comments immediately prior to the statement. Please help if you can.
I'm using PHP 4.3.3 with the Sablotron 0.98 extensions according to phpinfo().
Thanks,
Edwin
<?php
// Loads XML file along with associated XSLT stylesheet and performs
// transformation. File specification can be either absolute or
// a URL.
//
// Example usage: $feed = new xmlFeed ("http://rss.news.yahoo.com/rss/topstories", "XMLFeeds/ynfeed.xsl");
// print($feed->transform());
//
// Credit goes to Justin Grant and Bill Humphries for much of this code along with about a dozen other
// websites and/or newsgroup posts for little bits here and there.
class xmlFeed
{
// class constructor
function xmlFeed($xmlURL, $xslFName)
{
$this->xmlString = $this->readData($xmlURL);
$this->xslString = $this->readData($xslFName);
}
// loads data from a URL or a file and returns it as a string
function readData($loc)
{
$content = "\0";
$file = fopen($loc, "r");
while(!feof($file))
$content .= fgets($file, 1024);
fclose($file);
return $content;
}
// apply stylesheet transformation and return HTML result
function transform()
{
// create new XSLT processor, set base directory for loading XSLT file,
// and load arguments into array
$xslProcessor = xslt_create();
$arguments = array('/_xml' => $this->xmlString, '/_xsl' => $this->xslString);
// UNCOMMENT FOR TESTING XML AND XSLT STRINGS (these have what I expect in them)
// echo $this->xmlString;
// echo $this->xslString;
// perform XSLT transformation and print error, if it occurs
// CODE DIES ON NEXT STATEMENT: "Warning: Sablotron error on line 1:
// XML parser error 3: no element found in /home/theeds6/public_html/edwin/class.xmlFeed.php on line 54"
$result = xslt_process($xslProcessor, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
if(!$this->msg) print ("<P><STRONG>XSLT transformation failed.</STRONG></P>");
// clean up and return result
xslt_free($xslProcessor);
return $result;
}
}
?>