MENU
PHP XSLT
Apply an XSLT stylesheet to a loaded document with XSLTProcessor. Load the stylesheet into a DOMDocument and pass it to importStylesheet(), then transform the source document with transformToXML() (returns the result as a string), transformToDoc() (returns a DOMDocument), or transformToURI() (writes the result directly to a file or URI).XSLTProcessor
|
string getParameter(string $namespaceURI, string $localName) int XsltProcessor::getSecurityPrefs(void) bool hasExsltSupport(void) void importStylesheet(object $stylesheet) void registerPHPFunctions([mixed $restrict]) bool removeParameter(string $namespaceURI, string $localName) bool setParameter(string $namespace, string $name, string $value) bool setProfiling(string $filename) int XsltProcessor::setSecurityPrefs(int $securityPrefs) DOMDocument transformToDoc(DOMNode $doc) int transformToURI(DOMDocument $doc, string $uri) string transformToXML(DOMDocument $doc) |
For the source XML document and XSLT stylesheet used below, see Basic Flow Control (there named a.xml and a.xslt; the same content is loaded here as ab.xml/ab.xslt). transformToXML() returns the transformation result as a string, which prints correctly; transformToDoc() returns a DOMDocument object, which cannot be implicitly converted to a string by echo.
<?php
$xml = new DOMDocument;
$xml->load('ab.xml');
$xslt = new DOMDocument;
$xslt->load('ab.xslt');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslt);
echo $proc->transformToXML($xml); // string printed
echo $proc->transformToDoc($xml); // error
?>| 3 | Dog |
| 4 | Fish |
Catchable fatal error: Object of class DOMDocument could not be converted to string in C:\Program Files\Zend\Apache2\htdocs\xml\ab.php on line 9