note 78739 added to function.dom-domxpath-query

From: Date: Thu, 25 Oct 2007 13:46:25 +0000
Subject: note 78739 added to function.dom-domxpath-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-131726@lists.php.net to get a copy of this message
Unfortunately PHP's DOM extension doesn't support use of: <?xml-stylesheet type="text/xsl" ... ?> processing instruction. Here is an example, how to implement it using XPath query and extending DOMDocument by a method output(). <?php // This simple function adds missing direct usage of anonymous instances // in PHP5's reference model function a($var) { return $var; } // Extended DOMDocument class class MyDOMDocument extends DOMDocument { public function output() { $stylesheets = array(); $PIs = a(new DOMXPath($this)) ->query('/processing-instruction("xml-stylesheet")'); foreach($PIs as $PI) { // This might be implemented cleaner by regular parsing // of DOMProcessingInstruction::data property if(ereg('type *= *"text/xsl" +href *= *"([^"]+)"', $PI->data, $mem)) { // Here should be verified, that XSL file exists. a($stylesheets[] = new DOMDocument())->load($mem[1]); } } if($stylesheets) { $processor = new XSLTProcessor(); foreach($stylesheets as $stylesheet) $processor->importStylesheet($stylesheet); return $processor->transformToDoc($this); } // If no stylesheet instructions present, return self directly else return $this; } } ?> Usage: <?php $document = new MyDOMDocument(); $document->load('my.xml'); echo $document->output()->saveXML(); ?> With following file my.xml: <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="my.xsl" ?> <my-root /> and existing file my.xsl that code will transform the xml file using my.xsl and output the result. ---- Server IP: 147.32.160.17 Probable Submitter: 82.208.37.177 ---- Manual Page -- http://www.php.net/manual/en/function.dom-domxpath-query.php Edit -- https://master.php.net/note/edit/78739 Del: integrated -- https://master.php.net/note/delete/78739/integrated Del: useless -- https://master.php.net/note/delete/78739/useless Del: bad code -- https://master.php.net/note/delete/78739/bad+code Del: spam -- https://master.php.net/note/delete/78739/spam Del: non-english -- https://master.php.net/note/delete/78739/non-english Del: in docs -- https://master.php.net/note/delete/78739/in+docs Del: other reasons-- https://master.php.net/note/delete/78739 Reject -- https://master.php.net/note/reject/78739 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131726) next »