xslt_set_sax_handlers

(PHP 4 >= 4.0.6, PECL)

xslt_set_sax_handlers --  Imposta gli handler SAX da richiamare quando il document XML viene processato

Descrizione

void xslt_set_sax_handlers ( resource processor, array handler )

xslt_set_sax_handlers() registra gli handlers SAX per il documento, dato processor XSLT.

Il parametro handlers dovrebbe essere una matrice del seguente formato:

<?php

$handlers
= array(

  
"document" => array(
    
"start_doc",
    
"end_doc"),

  
"element"  => array(
    
"start_element",
    
"end_element"),

  
"namespace" => array(
    
"start_namespace",
    
"end_namespace"),

  
"comment"   => "comment",

  
"pi"        => "pi",

  
"character" => "characters"

);
?>

Dove le funzioni seguono la sintassi descritta per lo schema.

Nota: La matrice fornita non richiede di essere completa di tutti i differenti handler agli elementi sax (sebbene possa esserlo), ma richiede solo di essere conforme al formato "handler" => "function" descritto in precedenza.

Chiascuno degli handler SAX sono nel formato:

L'utilizzo di xslt_set_sax_handlers() non è molto differente rispetto ad un parser SAX tipo xml_parse() nelle trasformazioni xslt_process().

Esempi

Esempio 1. Esempio di uso xslt_set_sax_handlers()

<?php
// Da ohlesbeauxjours at yahoo dot fr
// Questo è un semplice esempio che applica strtoupper()
// sul contenuto di ciascun tag <auteur> e visualizza
// l'albero XML risultante:
    
$xml='<?xml version="1.0"?>
<books>
<book>
  <title>Mme Bovary</title>
  <author>Gustave Flaubert</author>
</book>
<book>
  <title>Mrs Dalloway</title>
  <author>Virginia Woolf</author>
</book>
</books>'
;

$xsl='<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="books/book">
  <livre>
   <auteur><xsl:value-of select="author/text()"/></auteur>
  </livre>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>'
;

// Handlers :
function start_document()
{
  
// inizia la lettura del coumento
}
      
function
end_document()
{
  
// fine lettura del documento
}
      
function
start_element($parser, $name, $attributes)
{
  global
$result,$tag;
  
$result .= "<". $name . ">";
  
$tag = $name;
}

function
end_element($parser, $name)
{
  global
$result;
  
$result .= "</" . $name . ">";
}

function
characters($parser, $data)
{
  global
$result,$tag;
  if (
$tag == "auteur" ) {
    
$data = strtoupper($data);
  }
  
$result .= $data;
}

// Trasformazione :
$xh = xslt_create();
$handlers = array("document" => array("start_document","end_document"),
   
"element" => array("start_element","end_element"),
   
"character" => "characters");

xslt_set_sax_handlers($xh, $handlers);
xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array("/_xml"=>$xml, "/_xsl"=>$xsl));
xslt_free($xh);
?>

Si può anche utilizzare xslt_set_object() per implementare i propri handler in un oggetto.

Esempio 2. Handler orientato agli oggetti

<?php
// Questa è una versione ad oggetti dell'esempi precedente
class data_sax_handler {

  var
$buffer, $tag, $attrs;

  var
$_xh;

  function
data_sax_handler($xml, $xsl)
  {
      
// la nostra risorsa xslt
      
$this->_xh = xslt_create();

      
xslt_set_object($this->_xs, $this);

      
// configura gli handler sax
      
$handlers = array(
        
"document" => array('start_document', 'end_document'),
        
"element" => array('start_element', 'end_element'),
        
"character" => 'characters'
      
);

      
xslt_set_sax_handlers($this->_xh, $handlers);

      
xslt_process($this->_xh, 'arg:/_xml', 'arg:/_xsl', NULL, array("/_xml"=>$xml, "/_xsl"=>$xsl));
      
xslt_free($this->_xh);


  }

  function
start_document()
  {
        
// inizia la lettura del documento
  
}

  function
end_document() {
        
// completa la lettura del documento
  
}
    
  function
start_element($parser, $name, $attributes) {
        
$this->tag = $name;
        
$this->buffer .= "<" . $name . ">";
        
$this->attrs = $attributes;
  }

  function
end_element($parser, $name)
  {
        
$this->tag = '';
        
$this->buffer .= "</" . $name . ">";
  }

  function
characters($parser, $data)
  {
    if (
$this->tag == 'auteur') {
          
$data = strtoupper($data);
    }
    
$this->buffer .= $data;
  }

  function
get_buffer() {
    return
$this->buffer;
  }
    
}
      
$exec = new data_sax_handler($xml, $xsl);

?>

Entrambi gli esempi visualizzeranno:

<livre>
   <auteur>GUSTAVE FLAUBERT</auteur>
</livre>
<livre>
   <auteur>VIRGINIA WOOLF</auteur>
</livre>