Zend - The PHP Company




XML

Add Code


XSLT Class (w/fallback mechanism for PHP @ 4.1.0)  

Type: class
Added by: kaoskind
Entered: 01/02/2002
Last modified: 02/12/2001
Rating: - (fewer than 3 votes)
Views: 6119
This class is a wrapper for the native xslt functions. There is a fallback mechanism that allows you to use every PHP 4 version, even though the function names have been changed since version 4.1


<?php 

/* $Id: xslt.class.php4,v 1.9 2002/01/31 21:57:15 sts Exp $ */

/**
* XSLT class
*
* Diese Klasse ist ein Wrapper f�r die XSLT Funktionen von PHP.
*
* Anwendungsbeispiel:
*   $xslt = new xslt( 
*                       "./style.xsl", 
*                       "./data.xml", 
*                       array( "name"=>$wert, 
*                              "name2"=>$wert2 ) 
*                    );
*   echo  $xslt->apply();
*
* @package   xslt
* @copyright 2001/2002 fase4.com. All rights reserved.
* @author    Stefan Saasen <s@fase4.com>
* @version   0.1 (2001-08-28) $Revision: 1.9 $
* @access    public
*/
class xslt {

    
/**
    * XSLT Datei
    *
    * @access private
    * @var    string
    */
    
var $_xslt "";
    
    
/**
    * XML Datei
    *
    * @access private
    * @var    string
    */    
    
var $_xml "";
    
    
/**
    * Dem XSLT Stylesheet zu �bergebende Parameter.
    * $params = array( "name"=>"wert, "name2"=>"wert" .... );
    *
    * @access private
    * @var    string
    */    
    
var $_xslt_params = array();
    
    
/**
    * PHP Version in der Form 4.x
    *
    * @access private
    * @var    string
    */    
    
var $_php_version 0;
    
    
    
/**
    * Konstruktor.
    *
    * @access    public
    * @author    Stefan Saasen <s@fase4.com>
    * @param     string $xsl XSLT Stylesheet
    * @param     string $xml XML Datei
    * @param     array  $xslt_params Dem XSLT Stylesheet 
    *                                zu �bergebende Parameter.
    * @see          _xslt, _xml, _xslt_params, _php_version
    */    
    
function xslt($xsl$xml$xslt_params = array() )
    {
        
$this->_xslt $xsl;
        
$this->_xml  $xml;
        
$this->_xslt_params $xslt_params;
        
$this->_php_version substr(phpversion(), 0,3);
    }
    
    
/**
    * Diese Funktion ruft die, abh�ngig von der jeweils installierten PHP
    * Version, passende Transformationsfunktion auf.
    *
    * @access    public
    * @author    Stefan Saasen <s@fase4.com>
    * @return    string XML Ergebnis der Transformation
    * @see          _process_old(), _process_new()
    */    
    
function apply()
    {
        if(
$this->_php_version 4.1) {
            return 
$this->_process_old();
        } else {
            return 
$this->_process_new();
        }
    }
    
    
/**
    * Diese Funktion wendet das XSLT Stylesheet auf die 
    * XML Datei an und gibt das Ergebnis oder eine 
    * Fehlermeldung zur�ck. Benutzt werden die Funktionen 
    * f�r PHP Versionen bis 4.1.0
    *
    * @access    private
    * @author    Stefan Saasen <s@fase4.com>
    * @return    string XML Ergebnis der Transformation
    * @see          _xslt, _xml, _xslt_params, apply() 
    */        
    
function _process_old()
    {
        
$_parser xslt_create();     
        if( !
xslt_run(   $_parser
                        
$this->_xslt
                        
$this->_xml
                        
"arg:/_result"
                        
$this->_xslt_params
                     
)) {
            
$this->_throwExceptionxslt_errno($_parser), 
                                   
xslt_error($_parser) );
        }
        
$result xslt_fetch_result($_parser); 
        
xslt_free($_parser);
        return 
$result;
    }

    
/**
    * Diese Funktion wendet das XSLT Stylesheet auf die 
    * XML Datei an und gibt das Ergebnis oder eine 
    * Fehlermeldung zur�ck. Benutzt werden die Funktionen
    * f�r PHP Versionen ab 4.1.0
    *
    *
    * @access    private
    * @author    Stefan Saasen <s@fase4.com>
    * @return    string XML Ergebnis der Transformation
    * @see          _xslt, _xml, _xslt_params, apply() 
    */        
    
function _process_new()
    {
        
$_parser xslt_create(); 
        if( !
$result = @xslt_process(  $_parser
                                       
$this->_xml,
                                       
$this->_xslt,  
                                       
NULL
                                       array(), 
                                       
$this->_xslt_params 
                                     
) ) {
            
$this->_throwExceptionxslt_errno($_parser), 
                                   
xslt_error($_parser) );
        }
        
xslt_free($_parser);
        return 
$result;
    }
    
   
/**
    * Diese Funktion wendet das XSLT Stylesheet auf die 
    * XML Datei an und gibt das Ergebnis oder eine 
    * Fehlermeldung zur�ck. Benutzt werden die Funktionen 
    * f�r PHP Versionen ab 4.1.0
    *
    * @access    private
    * @author    Stefan Saasen <s@fase4.com>
    * @param     string $errno Fehlernummer
    * @param     string $error Fehlermeldung
    */        
    
function _throwException$errno$error )
    {
        echo 
"<b>Sablotron Error</b><br /><br />";
        echo 
"<span style="color:#660000;font-weight:bold;">(" . 
                    
$errno"): " .
        
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" 
                    
$error "</span>";    
    }
}
?>


Usage Example




Rate This Script





Search



This Category All Categories