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 331 (string $namespaceURI, string $localName)
int XsltProcessor::getSecurityPrefs 331 (void)
bool hasExsltSupport 331 (void)
void importStylesheet 331 (object $stylesheet)
void registerPHPFunctions 331 ([mixed $restrict])
bool removeParameter 331 (string $namespaceURI, string $localName)
bool setParameter 331 (string $namespace, string $name, string $value)
bool setProfiling 331 (string $filename)
int XsltProcessor::setSecurityPrefs 331 (int $securityPrefs)
DOMDocument transformToDoc 331 (DOMNode $doc)
int transformToURI 331 (DOMDocument $doc, string $uri)
string transformToXML 331 (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
?>
Output
A B C
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