note 103368 deleted from xsltprocessor.transformtoxml by danbrown
| From: | danbrown@php.net | Date: | Mon, 11 Apr 2011 15:38:45 +0000 |
| Subject: | note 103368 deleted from xsltprocessor.transformtoxml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178703@lists.php.net to get a copy of this message | ||
Note Submitter: zweibieren at yahoo dot com
----
My previous note, just below fails when data entries have HTML entity references. (Some database
entries may even contain user generated text.) libxml has the pedantic habit of throwing a FATAL
error for any undefined entitiy. Solution: hide the entities so libxml doesn't see them.
function hideEntities($data) {
return str_replace("&", "&", $data);
}
You could just add this to the example, but it is tidier to define a function to load data into a
DOMDocument. This way you don't need entity declarations in catalog.xsl, either.
// Added function for Example #1
/** Load an XML file and create a DOMDocument.
Handles arbitrary entities, even undefined ones.
*/
function fileToDOMDoc($filename) {
$dom= new DOMDocument;
$xmldata = file_get_contents($filename);
$xmldata = str_replace("&", "&", $xmldata); // disguise &s
going IN to loadXML()
$dom->substituteEntities = true; // collapse &s going OUT to transformToXML()
$dom->loadXML($xmldata);
return $dom;
}
// Compare with Example #1 Transforming to a string
// Load the XML sources
$xml = fileToDOMDoc('collection.xml');
$xsl = fileToDOMDoc('collection.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// transform $xml according to the stylesheet $xsl
echo $proc->transformToXML($xml); // transform the data