note 61479 deleted from ref.xslt by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:37:34 +0000 |
| Subject: | note 61479 deleted from ref.xslt by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207384@lists.php.net to get a copy of this message | ||
Note Submitter: gabriel a-t bumpt do-t net
----
If you need to transform an XML document that specifies the URL of its own prefered stylesheet
(using the processing instruction <?xml-stylesheet href="xyz.xsl"?>, you can start
by parsing the XML data to retrieve that URL, and then call the xslt_process function using the
specified stylesheet :
<?php
/* A small class for stylesheet URL extraction from an XML */
class XMLStylesheet
{
var $hParser;
var $xml;
var $stylesheet;
function XMLStylesheet($filename)
{
$this->hParser = xml_parser_create();
xml_set_object($this->hParser, $this);
xml_set_processing_instruction_handler($this->hParser, 'fnProcessingInstruction');
$this->xml = file_get_contents($filename);
xml_parse($this->hParser, $this->xml, true);
xml_parser_free($this->hParser);
}
function getStylesheet()
{
return $this->stylesheet;
}
function fnProcessingInstruction($hParser, $target, $data)
{
if($target == 'xml-stylesheet')
{
ereg('href="([^"]+)"', $data, $regs);
$this->stylesheet = $regs[1];
}
}
}
/* Now, instanciate an object of this class, given the filename of our XML data */
$xml = new XMLStylesheet('my_data.xml');
/* And finally, retrieve the URL of the stylesheet specified in the XML data. */
$xsl_url = $xml->getStylesheet());
/* Now you can xslt_process as usual. */
?>