note 103368 added to xsltprocessor.transformtoxml

From: Date: Mon, 11 Apr 2011 02:07:54 +0000
Subject: note 103368 added to xsltprocessor.transformtoxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-178669@lists.php.net to get a copy of this message
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 ---- Server IP: 208.69.120.55 Probable Submitter: 67.186.6.197 ---- Manual Page -- http://www.php.net/manual/en/xsltprocessor.transformtoxml.php Edit -- https://master.php.net/note/edit/103368 Del: integrated -- https://master.php.net/note/delete/103368/integrated Del: useless -- https://master.php.net/note/delete/103368/useless Del: bad code -- https://master.php.net/note/delete/103368/bad+code Del: spam -- https://master.php.net/note/delete/103368/spam Del: non-english -- https://master.php.net/note/delete/103368/non-english Del: in docs -- https://master.php.net/note/delete/103368/in+docs Del: other reasons-- https://master.php.net/note/delete/103368 Reject -- https://master.php.net/note/reject/103368 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178669) next »