«^»
4.6. Manipulating XML documents

XML is a markup language that allows data to be described using your own tags.

PHP comes with an XML parser that allows you to write PHP code that walks through an XML document. It generates events for each part of the document, generating an event for each start tag, for each end tag, and for each occurrence of characters appearing between a start tag and an end tag. In a PHP program, you can nominate functions to be called when these events occur. Note that each part of the XML document is visited only once. If instead you want the ability to wander around an XML document, it is possible to put the representation of the XML document into an array. For more details about parsing XML documents from PHP, look at my document 'Processing XML using PHP': http://www.dur.ac.uk/barry.cornelius/papers/xml.processing.using.php/

PHP provides an extension that can do XSL transformations. In order to use this extension, the person installing PHP needs to build it with a library that implements XSLT. Suppose we have a file (consumables.xml) that contains the following XML document:

0206: <?xml version="1.0"?>
0207: <consumables>
0208:    <product>
0209:       <category>cassette tape</category>
0210:       <item>DC6150 cartridge</item>
0211:       <price>9.50</price>
0212:    </product>
0213:    <product>
0214:       <category>cassette tape</category>
0215:       <item>DC4-60</item>
0216:       <price>6.00</price>
0217:    </product>
        ...
0428:    <product>
0429:       <category>writeable media</category>
0430:       <item>writeable CD</item>
0431:       <price>1.75</price>
0432:    </product>
0433: </consumables>

Also, suppose the following XSL instructions have been stored in a file called consumables.xsl:

0434: <?xml version="1.0"?>
0435: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
0436: <xsl:output method="html"/>
0437: <xsl:template match="consumables">
0438:    <html>
0439:    <body>
0440:    <table>
0441:    <tr bgcolor="yellow">
0442:    <td>category</td>
0443:    <td>item</td>
0444:    <td>price</td>
0445:    </tr>
0446:       <xsl:apply-templates/>
0447:    </table>
0448:    </body>
0449:    </html>
0450: </xsl:template>
0451: <xsl:template match="product">
0452:    <tr>
0453:    <td><xsl:value-of select="category"/></td>
0454:    <td><xsl:value-of select="item"/></td>
0455:    <td><xsl:value-of select="price"/></td>
0456:    </tr>
0457: </xsl:template>
0458: </xsl:stylesheet>
Here is a PHP script that takes the consumables.xml file and transforms it using the rules in the consumables.xsl file:
0459: <HTML><BODY>
0460: <?php
0461:    chdir('/users/dcl0bjc/public_html/papers/phpintro/code');
0462:    $xsltproc = xslt_create();
0463:    $html = xslt_process($xsltproc,'consumables.xml','consumables.xsl');
0464:    if (!$html) {
0465:       die('XSLT processing error:' . xslt_error($xsltproc));
0466:    }
0467:    xslt_free($xsltproc);
0468:    echo $html;
0469: ?> 
0470: </BODY></HTML>

Go to the script at: http://www.dur.ac.uk/barry.cornelius/papers/phpintro/code/xslt.php

My document 'Processing XML using PHP' gives more details about performing XSL transformations in PHP. It is available at: http://www.dur.ac.uk/barry.cornelius/papers/xml.processing.using.php/