Recipe 12.7. Transforming XML with XSLT


12.7.1. Problem

You have an XML document and an XSL stylesheet. You want to transform the document using XSLT and capture the results. This lets you apply stylesheets to your data and create different versions of your content for different media.

12.7.2. Solution

Use PHP's XSLT extension:

// Load XSL template $xsl = newDOMDocument; $xsl->load('stylesheet.xsl'); // Create new XSLTProcessor $xslt = new XSLTProcessor(); // Load stylesheet $xslt->importStylesheet($xsl); // Load XML input file $xml = new DOMDocument; $xml->load('data.xml'); // Transform to string $results = $xslt->transformToXML($xml); // Transform to a file $results = $xslt->transformToURI($xml, 'results.txt'); // Transform to DOM object $results = $xslt->transformToDoc($xml);

The transformed text is stored in $results.

12.7.3. Discussion

XML documents describe the content of data, but they don't contain any information about how that data should be displayed. However, when XML content is coupled with a stylesheet described using XSL (eXtensible Stylesheet Language), the content is displayed according to specific visual rules.

The glue between XML and XSL is XSLT (eXtensible Stylesheet Language Transformations). These transformations apply the series of rules enumerated in the stylesheet to your XML data. So just as PHP parses your code and combines it with user input to create a dynamic page, an XSLT program uses XSL and XML to output a new page that contains more XML, HTML, or any other format you can describe.

There are a few XSLT programs available, each with different features and limitations. PHP 5 supports only the libxslt processor. This is a different processor than PHP 4 used.

Using XSLT in PHP 5 involves two main steps: preparing the XSLT object and then triggering the actual transformation for each XML file.

To begin, load in the stylesheet using DOM. Then, instantiate a new XSLTProcessor object, and import the XSLT document by passing in your newly created DOM object to the importStylesheet( ) method, as shown in Example 12-14.

Configuring the XSLT processor

// Load XSL template $xsl = newDOMDocument; $xsl->load('stylesheet.xsl'); // Create new XSLTProcessor $xslt = new XSLTProcessor(); // Load stylesheet $xslt->importStylesheet($xsl);

Now the transformer is up and running. You can transform any DOM object in one of three ways: into a string, into a file, or back into another DOM object, as shown in Example 12-15.

Transforming the XML data

// Load XML input file $xml = new DOMDocument; $xml->load('data.xml'); // Transform to string $results = $xslt->transformToXML($xml); // Transform to a file $results = $xslt->transformToURI($xml, 'results.txt'); // Transform to DOM object $results = $xslt->transformToDoc($xml);

When you call transformToXML( ) or transformToDoc( ), the extension returns the result string or object. In contrast, transformToURI( ) returns the number of bytes written to the file, not the actual document.

These methods return false when they fail, so to accurately check for failure, write:

if (false === ($results = $xslt->transformToXML($xml))) {     // an error occurred }

Using === prevents a return value of 0 from being confused with an actual error.

12.7.4. See Also

Documentation on XSL functions at http://www.php.net/xsl; XSLT by Doug Tidwell (O'Reilly).




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net