The Register® — Biting the hand that feeds IT

Harness XML with PHP 5 extensions

A time to query

Free whitepaper – Best practices for optimizing performance and availability in virtual infrastructures

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.®

Free whitepaper – The top 5 server monitoring battles

Don’t Miss

Microsoft Office logoOffice 2010 fights Google with SharePoint bloat

Review Decent upgrade gets out of shape

Ubuntu teaser Ubuntu's Karmic Koala bares fangs at Windows 7

Review Shuttleworthian scrap

AppleChange your views: OS X tags exploited

Mac Secrets Apple windows insider

MicrosoftMicrosoft 'Dallas' muscles Google data crusade

PDC Crunches Red Planet