This article is more than 1 year old

Harness XML with PHP 5 extensions

A time to query

Transform XML

To transform the example XML document with an XSLT, catalog.xslt, using the XSL extension, create a PHP script, transformXML.php. Create a DOMDocument object and load the example XML document. Create another DOMDocument for the stylesheet and load the XSLT file.

$xslFile=new DOMDocument();
$xslFile->load("file://C:/PHP/catalog.xslt");

An XML document is transformed using an XSLTProcessor. Therefore, create an XSLTProcessor.

$xsltProcessor=new XSLTProcessor();

An XSLT stylesheet is imported into the XSLT processor for transformations with importStylesheet(DOMDocument) .

$xsltProcessor->importStylesheet($xslFile);

Transform the XML document with the XSLT file using transformToXML() , and output the result of the transformation.

echo  $xsltProcessor->transformToXML($domDocument);

The output from the XSLT transformation gets displayed in the browser.

There you have it: you have just parsed, navigated, transformed and validated an XML document using the PHP 5 XML extensions. As noted, my PHP 5 scripts were run in Apache but you can use any web server that supports PHP.®

More about

TIP US OFF

Send us news


Other stories you might like