Hey Guys,
Im new to the world of XML and XSL and wanted to play around with taking RSS feeds from other sites, and basically reformatting them.
I found this class: http://www.phpclasses.org/browse/package/498.html
Which looks awesomly simple to begin using but Im having some problems due to my limited knowledge.
The demo package which can be d/loaded from the above site contains four scripts as outlined below:
the index file:
<?php
include("class.xslt.php");
$t = new XSLTransformer('feed.xml', 'test.xsl');
echo $t->getOutput();
$t->destructXSLTansform();
?>
the xsl file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1"/>
<xsl:template match="person">
<html>
<head>
<title><xsl:value-of select="name"/></title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table border="0" cellspacing="0" cellpadding="0">
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="name">
<tr>
<td><b>Name</b></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
<xsl:template match="address">
<tr>
<td valign="top"><b>Adress</b></td>
<td>
<xsl:value-of select="./street"/><br/>
<xsl:value-of select="./ort"/><br/>
<xsl:value-of select="./country"/><br/>
</td>
</tr>
</xsl:template>
<xsl:template match="tel">
<tr>
<td><b>Tel</b></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
<xsl:template match="email">
<tr>
<td><b>Email</b></td>
<td><a><xsl:attribute name="href">mailto:<xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></a></td>
</tr>
</xsl:template>
<xsl:template match="url">
<tr>
<td><b>Url</b></td>
<td><a><xsl:attribute name="href"><xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></a></td>
</tr>
</xsl:template>
</xsl:stylesheet>
the xml file:
<?xml version="1.0"?>
<person>
<name>Tim Berners-Lee</name>
<address>
<street>200 Technology Square</street>
<ort>Cambridge MA 02139</ort>
<country>USA </country>
</address>
<tel>+1 (617) 253 5702</tel>
<email>timbl@w3.org</email>
<url>http://www.w3.org/People/Berners-Lee/</url>
</person>
the class itself:
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This code is released under the GNU LGPL Go read it over here: |
// | http://www.gnu.org/copyleft/lesser.html |
// +----------------------------------------------------------------------+
// | Authors: Mathias Sulser <suls@suls.org> |
// +----------------------------------------------------------------------+
//
// $Id: class.xslt.php,v 1.6 2002/03/20 19:26:40 eeshq Exp $
//
/**
* Extensible Stylesheet Language Transformations - XSLTansformer
*
* With this class you can easy transform XML files to a HTML page.
* The only requirements are a a XML parser (i.e Expat) and a XSLT
* processor (Sablotron).
*
* @author Mathias Sulser <suls@suls.org>
* @access public
* @version $Id: class.xslt.php,v 1.6 2002/03/20 19:26:40 eeshq Exp $
* @package XSLT
*/
class XSLTransformer {
/**
* XSLT processor resource
* @var mixed $_processor
*/
var $_processor;
/**
* Contains the XSL-data from a file or variable
* @var string $_xsl
*/
var $_xsl;
/**
* Contains the XML-data from a file or variable
* @var string $_xml
*/
var $_xml;
/**
* The parameters can be accessed in the XSL file
* @var array $_params
*/
var $_params = array();
/**
* Arguments for the XSLT process
* @var array $_arguments
*/
var $_arguments = array();
/**
* Contains all Errors of the parsing process
* @var array $_error
*/
var $_error = array();
/**
* Output of the parsed XML and XSL files
* @var string $_output
*/
var $_output;
/**
* That's the constructor of this class.
*
* @param string $xml XML data (file or variable)
* @param string $xsl XSL data (file or variable)
* @param array $params Parameters for the XSL file
* @access public
*/
function XSLTransformer($xml, $xsl, $params = array()) {
$this->_processor = xslt_create();
$this->_setVars($xml, $xsl, $params);
$this->_setArguments();
$this->_transform();
}
/**
* Destructor who frees the used memory
*
* @access public
*/
function destructXSLTansform() {
xslt_free($this->_processor);
unset($this->_xsl);
unset($this->_xml);
unset($this->_params);
unset($this->_arguments);
unset($this->_output);
unset($this->_processor);
}
/**
* Sets the output
*
* @access private
* @param mixed
*/
function _setOutput($string) {
$this->_output = $string;
}
/**
* Returns the result of the transform process
*
* @access public
* @return mixed $_output
*/
function getOutput() {
return $this->_output;
}
/**
* Stores the XML and XSL data
*
* @param string $xmlData XML data (File or Variable)
* @param string $xslData XSL data (File or Variable)
* @param array $parameters Parameters for the XSL file
* @access private
*/
function _setVars($xmlData, $xslData, $parameters = array()) {
if(file_exists($xmlData)) {
$this->_xml = join('', file($xmlData));
} else {
$this->_xml = $xmlData;
}
if(file_exists($xslData)) {
$this->_xsl = join('', file($xslData));
} else {
$this->_xsl = $xslData;
}
$this->_params = $parameters;
}
/**
* Sets the value of $this->_arguments
*
* @access private
*/
function _setArguments() {
$this->_arguments = array (
"/_xml" => $this->_xml,
"/_xsl" => $this->_xsl
);
}
/**
* Returns the value of $this->_arguments
*
* @access private
* @return array $_arguments
*/
function _getArguments() {
return $this->_arguments;
}
/**
* Returns the value of $this->_params
*
* @access private
* @return array $_params
*/
function _getParameters() {
return $this->_params;
}
/**
* Starts the transform process
*
* @access private
*/
function _transform() {
if(empty($this->_params)) {
$result = @xslt_process($this->_processor, 'arg:/_xml', 'arg:/_xsl', NULL, $this->_getArguments());
} else {
$result = @xslt_process($this->_processor, 'arg:/_xml', 'arg:/_xsl', NULL, $this->_getArguments(), $this->_getParameters());
}
if($result) {
$this->_setOutput($result);
} else {
$this->_setError("Cannot process XSLT document ".xslt_errno($this->_processor).": ".xslt_error($this->_processor));
$this->_setOutput($this->_getError());
}
}
/**
* Creates a new error and stores the older ones
*
* @access private
* @param string $string Set the error description
*/
function _setError($string) {
if(sizeof($this->_error)) {
$newError = sizeof($this->_error);
$this->_error[$newError] = $string;
} else {
$this->_error[0] = $string;
}
}
/**
* Makes the errors ready for print
*
* @return string Set's the errors ready for output
* @access private
*/
function _getError() {
foreach ($this->_error as $value) {
$help[] = "<b>Error </b>: $value
";
}
return implode("<br>", $help);
}
} // end of class XSLTransformer
?>
These installed and ran fine, so i though I’d be adventurouse and try customsing it to a feed on the net …this one: http://feeds.sophos.com/en/rss2_0-sophos-latest-viruses.xml
This resulted in the following out put:
http://w1red.com/xml/
I cant for the life of me work out how to format the first section, that isnt held within item tags but is with the CONTENT tags. Im not looking to remove the © info just to be able to control and format it to blend in with the rest of the info.
Below is my customised xsl doc:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1"/>
<xsl:template match="channel/title">
<html>
<head>
<title><xsl:value-of select="."/></title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table border="0" cellspacing="0" cellpadding="0">
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<tr>
<td><b>Title:</b></td>
<td><xsl:value-of select="../title"/></td>
<td valign="top"><b>Link:</b></td>
<td>
<xsl:value-of select="../link"/><br/>
</td></tr>
<tr>
<td><b>Title:</b></td>
<td><xsl:value-of select="title"/></td>
<td valign="top"><b>Link:</b></td>
<td>
<xsl:value-of select="link"/><br/>
</td></tr>
</xsl:template>
</xsl:stylesheet>
As always …anyone who can helps time is much much appreciated
If you need any othe rinfo please dont hesitate to ask
Zaid