[phplib] Extended DB_Sql to support XSLT From: Bryan Brunton (bryanbrun <email protected>)
Date: 02/27/01

Here is an example of how I have extended DB_Sql to support XSLT. For comparison, below my code
is an example of PHP's sablot extension:

$db = new DB_Sql_Xml;
$db->query("SELECT * from levels order by level_id");

$xslData_2 = '<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="resultset"><h2><xsl:value-of
select="rank"/></h2></xsl:template></xsl:stylesheet>';

if ($db->set_xsl($xslData_2) && $db->set_xml($db->get_xml())) {
        $db->transform();
        echo $db->get_output();
}

$xslData = '<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="article"><h2><xsl:value-of
select="title"/></h2></xsl:template></xsl:stylesheet>';
$xmlData = '<?xml version="1.0"?><article><title>Learning German</title><author>Sterling
Hughes</author><body>Essential phrases:</body></article>';

if (xslt_process($xslData, $xmlData, $result)) {
    echo "Here is the brilliant in-depth article on learning";
    echo " German: ";
    echo "<br>\n<br>";
    echo $result;
} else {
    echo "There was an error that occurred in the XSL transformation...\n";
    echo "\tError number: " . xslt_errno() . "\n";
    echo "\tError string: " . xslt_error() . "\n";
    exit;
}

Here is the extension to DB_Sql. Included is a doc_reader class (much of this code was taken from
an article on phpbuilder.com) that supports xsl and xml files via local and http:// access. This
code hasn't been tested much beyond simple functionality.

class DB_Sql_Xml extends DB_Sql {
  var $query_string = "";
  var $xsl = "";
  var $xml = "";
  var $output = "";
  var $error = "";
  var $processor;
        
  function xsl_transformer() {
    $this->processor = xslt_create();
  }

  function destroy() {
    xslt_destroy($this->processor);
  }
  
  function set_output($string) {
    $this->output = $string;
  }

  function get_output() {
    return $this->output;
  }

  function set_xml($uri) {
    if ( $uri[0] == "<" ) {
      $this->xml = $uri;
      return true;
    } else {
      if($doc = new doc_reader($uri)) {
        $this->xml = $doc->get_string();
        return true;
      } else {
        $this->set_error("Could not open $uri");
        return false;
      }
    }
  }

  function set_xsl($uri) {
    if ( $uri[0] == "<" ) {
      $this->xsl = $uri;
      return true;
    } else {
      if($doc = new doc_reader($uri)) {
        $this->xsl = $doc->get_string();
        return true;
      } else {
        $this->set_error("Could not open $uri");
        return false;
      }
    }
  }

  function transform() {
    xslt_process($this->xsl, $this->xml, $result);
    $this->set_output($result);
  }

  function set_error($string) {
    $this->error = $string;
  }

  function get_error() {
    return $this->error;
  }
  
  function xml_encode($text) {
    $search = array ("'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i");
    $replace = array ( "\"", "&", "<", ">");

    return preg_replace ($search, $replace, $text);
  }

  function get_xml() {
    $str = "";
                
    $str = $str . '<?xml version="1.0"?>';
    $str = $str . '<resultset statement="' . $this->query_string . '">';

    $this->Record =  <email protected>($this->Query_ID, $this->Row++, 1);

    while (list($key, $val) = each($this->Record)) {
      $str = $str . '<' . $key . '>' . $this->xml_encode($val) . '</' . $key . '>';
    }

    $str = $str . '</resultset>';
    return $str;
  }
}

class doc_reader {
  var $string = "";
  var $type = "";
  var $bignum = 1000000;
        var $uri = "";

  function doc_reader($uri) {
    $this->set_uri($uri);
    $this->set_type();

    $fp = fopen($this->get_uri(),"r");

    if($fp) {
      if ($this->get_type() == 'file') {
        $length = filesize($this->get_uri());
      } else {
        $length = $this->bignum;
      }

      $this->set_string(fread($fp,$length));
      return 1;
    } else {
      return 0;
    }
  }

  function is_file($uri) {
    if (strstr($uri,'http://') == $uri) {
      return false;
    } else {
      return true;
    }
  }

  function set_uri($string) {
    $this->uri = $string;
  }

  function get_uri() {
    return $this->uri;
  }

  function set_string($string) {
    $this->string = $string;
  }

  function get_string() {
    return $this->string;
  }

  function set_type() {
    if ($this->is_file($this->uri)) {
      $this->type = 'file';
    } else {
      $this->type = 'url';
    }
  }

  function get_type() {
    return $this->type;
  }
}

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>