PHP XML Transforming XML with XSLT - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Transforming XML with XSLT - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 15, 2019

PHP XML Transforming XML with XSLT

PHP XML



Transforming XML with XSLT

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.

Solution

Use PHP’s XSLT extension:

        // Load XSL template
        $xsl = new DOMDocument;
        $xsl->load(__DIR__ . '/stylesheet.xsl');

        // Create new XSLTProcessor
        $xslt = new XSLTProcessor();
        // Load stylesheet
        $xslt->importStylesheet($xsl);

        // Load XML input file
        $xml = new DOMDocument;
        $xsl->load(__DIR__ . '/address-book.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.

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 supports only the libxslt processor.

Using XSLT in PHP 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:

        // Load XSL template
        $xsl = new DOMDocument;
        $xsl->load(__DIR__ . '/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:

        // Load XML input file
        $xml = new DOMDocument;
        $xsl->load(__DIR__ . '/stylesheet.xsl');

        // 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 resulting 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.


No comments:

Post a Comment

Post Top Ad