From 6d8d5e3cb38fb7f9eab3069f2eefeb655b4527d1 Mon Sep 17 00:00:00 2001 From: Mehdi Achour Date: Sat, 24 Jul 2004 17:11:30 +0000 Subject: [PATCH] document xslt_set_sax_handlers # work based on user note and Sterling Hughes notes in php-src git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@164219 c90b9560-bf6c-de11-be94-00142212c4b1 --- .../xslt/functions/xslt-set-sax-handlers.xml | 313 +++++++++++++++++- 1 file changed, 311 insertions(+), 2 deletions(-) diff --git a/reference/xslt/functions/xslt-set-sax-handlers.xml b/reference/xslt/functions/xslt-set-sax-handlers.xml index b63299cc34..aaf1391023 100644 --- a/reference/xslt/functions/xslt-set-sax-handlers.xml +++ b/reference/xslt/functions/xslt-set-sax-handlers.xml @@ -1,5 +1,5 @@ - + @@ -16,7 +16,316 @@ arrayhandlers - &warn.undocumented.func; + xslt_set_sax_handlers registers the SAX + handlers for the document, given a XSLT + processor resource. + + + handlers should be an array in the following format: + + + array( + "start_doc", + "end_doc"), + + "element" => array( + "start_element", + "end_element"), + + "namespace" => array( + "start_namespace", + "end_namespace"), + + "comment" => "comment", + + "pi" => "pi", + + "character" => "characters" + +); +?> +]]> + + + Where the functions follow the syntax described for the scheme handler + functions. + + + + The given array does not need to contain all of the different sax + handler elements (although it can), but it only needs to conform to + "handler" => "function" format described above. + + + + Each of the individual SAX handler functions are in the format below: + + + + + start_doc + resourceprocessor + + + + + + + end_doc + resourceprocessor + + + + + + + start_element + resourceprocessor + stringname + arrayattributes + + + + + + + end_element + resourceprocessor + stringname + + + + + + + start_namespace + resourceprocessor + stringprefix + stringuri + + + + + + + end_namespace + resourceprocessor + stringprefix + + + + + + + comment + resourceprocessor + stringcontents + + + + + + + pi + resourceprocessor + stringtarget + stringcontents + + + + + + + characters + resourceprocessor + stringcontents + + + + + + + Using xslt_set_sax_handlers doesn't look very different than + running a SAX parser like xml_parse on the result of an + xslt_process transformation. + + + + <function>xslt_set_sax_handlers</function> Example + + tag and then displays the +// resulting XML tree: + +$xml=' + + + Mme Bovary + Gustave Flaubert + + + Mrs Dalloway + Virginia Woolf + +'; + +$xsl=' + + + + + + + + + +'; + +// Handlers : +function start_document() +{ + // start reading the document +} + +function end_document() +{ + // end reading the document +} + +function start_element($parser, $name, $attributes) +{ + global $result,$tag; + $result .= "<".$name.">"; + $tag = $name; +} + +function end_element($parser, $name) +{ + global $result; + $result .= ""; +} + +function characters($parser, $data) +{ + global $result,$tag; + if($tag=="auteur") { + $data=strtoupper($data); + } + $result .= $data; +} + +// Transformation : +$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); +?> +]]> + + + + + Every values of the handlers array is either a + string containing the function name, or an array in the following format: + array(&$obj, "method"). + + + + Object oriented handler + +tag = $tagname; + $this->attrs = $attributes; + } + + function end_element($parser, $tagname) + { + $this->tag = ''; + } + + function characters($parser, $data) + { + // Are we calling a PHP function ? + if ($this->tag == 'php') { + $function = $this->attrs['function']; + $data = $function($data); + } + $this->buffer .= $data; + } + + function get_buffer() { + return $this->buffer; + } + +} + +// our xsl handle +$xh = xslt_create(); + +$handler = new data_sax_handler(); +// configure sax handlers +$handlers = array( + "document" => array( + array(&$handler, 'start_document'), + array(&$handler, 'end_document') +), + "element" => array( + array(&$handler, 'start_element'), + array(&$handler, 'end_element') +), + "character" => array(&$handler, 'characters') +); + +xslt_set_sax_handlers($xh, $handlers); + +xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array("/_xml"=>$xml, "/_xsl"=>$xsl)); +xslt_free($xh); + +?> +]]> + + + Both examples will output: + + + + GUSTAVE FLAUBERT + + + VIRGINIA WOOLF + +]]> + +