note 31687 added to function.xslt-set-sax-handlers
| From: | ohlesbeauxjours at yahoo dot fr | Date: | Wed, 30 Apr 2003 10:46:48 +0000 |
| Subject: | note 31687 added to function.xslt-set-sax-handlers | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47286@lists.php.net to get a copy of this message | ||
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.
By the way, after a few tests, I found that performances were basically the same, between those two
SAX parsers (xslt_set_sax_handlers and xml_parse)
Small difference yet : it seems that xslt_set_sax_handlers() treats more efficiently the breaklines
in the XML source.
Even with XML_OPTION_SKIP_WHITE set to 1, xml_parse() uselessly calls xml_set_element_handler() for
every breakline or white space placed between closing and opening tags.
xslt_set_sax_handlers() doesn't.
But xslt_set_sax_handlers is rather buggy on versions of PHP prior to 4.3
Here is a (very) stupid sample that applies strtoupper() on the content of every tag <auteur>,
and then displays the resulting XML tree :
$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 () {
// start reading the document
}
function end_document () {
// complete reading the document
}
function start_element($parser, $name, $data) {
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;
}
// 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));
echo htmlentities($result);
xslt_free($xh);
--
http://www.php.net/manual/en/function.xslt-set-sax-handlers.php
http://master.php.net/manage/user-notes.php?action=edit+31687
http://master.php.net/manage/user-notes.php?action=delete+31687
http://master.php.net/manage/user-notes.php?action=reject+31687