alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow PHP Oracle arrow Transforming and Processing XML with XSLT
Site Search


Transforming and Processing XML with XSLT

Transforming and Processing XML with XSLT

In the preceding example, you transform XML into HTML directly in your script, wrapping the data extracted from the XML document into appropriate HTML tags. Alternatively, you might perform an XSL (Extensible Stylesheet Language) transformation to get the same general results.

However, before you can use the XSL extension you have to enable it in your PHP installation.

In UNIX, you have to recompile PHP with the following flag:

--with-xsl

In Windows, you have to uncomment the following line in the php.ini configuration fi le and then restart the Apache/PHP server:

extension=php_xsl.dll

Once you have enabled the XSL extension, you can use XSL functions to transform XML into HTML or another XML or a variety of other formats. The following figure depicts the general steps performed by a PHP/Oracle application that generates an HTML page with PHP, based on the result set retrieved from the database.

Here is the explanation of the steps in the above figure:

  • The script queries the database to retrieve the data that will be used to construct an XML document.
  • The script generates the XML document using the PHP DOM extension, based on the data retrieved in Step 1.
  • The script transforms the XML document generated in step 2 into HTML format with the PHP XSL extension.
  • The script posts the HTML page generated in step 3 to the user's browser.

As you can see, most of the XML processing work in the above scenario is performed by the PHP engine on the web server rather than on the database server. So, this may be efficient in cases where the database server becomes a performance bottleneck in your system.

Using this scenario, you might transform the employees XML document shown in the Creating XML with the DOM PHP Extension section into HTML so that the result page looks like the following figure:

If you want to get the page shown in the above figure by applying an XSL transformation to the employees XML document, you first have to create an XSLT stylesheet describing the way the data is to be transformed.

The following employees.xsl stylesheet might be used to transform the employees XML document into HTML to get the page shown in the above figure.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
  3. Transform">
  4. <xsl:template match="/">
  5. <title>Employees</title>
  6. </head>
  7. <font face="Arial">
  8. <h2>List of employees from employees.xml</h2>
  9. <table border="1" cellspacing="0" cellpadding="5">
  10. <th><b>EMPLOYEE ID</b></th>
  11. <th><b>LAST NAME</b></th>
  12. <th><b>SALARY</b></th>
  13. </tr>
  14. <xsl:for-each select="EMPLOYEES">
  15. <xsl:for-each select="EMPLOYEE">
  16. <td><xsl:value-of select="@id"/></td>
  17. <td><xsl:value-of select="ENAME"/></td>
  18. <td><xsl:value-of select="SALARY"/></td>
  19. </tr>
  20. </xsl:for-each>
  21. </xsl:for-each>
  22. </table>
  23. </font>
  24. </body>
  25. </html>
  26. </xsl:template>
  27. </xsl:stylesheet>
 

As you can see, the XSLT stylesheet shown in the listing is an XML document that contains elements and attributes defined in the XSLT namespace: http://www.w3.org/1999/XSL/Transform. Whereas the intent of these elements and attributes is to provide instructions to an XSLT processor, the HTML tags also presented in the stylesheet will be directly added to the resultant XML document.

While the employees.xsl stylesheet shown in the listing is designed to simply transform an employees XML document into HTML, you might create a more complicated stylesheet that would process XML data included in that XML document.

NOTE: It is interesting to note that XSLT is not limited to transforming XML data?it also can be used to process XML. Sometimes, performing the XML processing with XSLT may be much easier than using DOM operations to do the same job. For example, with XSLT, to calculate the total of all the orders included in the document, you don't need to write the code that will iterate over all the elements representing that orders, as you would with the DOM approach. Instead, you might use the xsl: value-of select element with the sum function in your stylesheet to get the job done.

Turning back to the employees.xsl stylesheet, suppose you want to add another column to the resultant HTML table, say, BONUS whose values are calculated based on the values from the SALARY column. In that case, the fragment of the stylesheet responsible for generating the HTML table might be modified as follows:

  1. <table border="1" cellspacing="0" cellpadding="5">
  2. <th><b>EMPLOYEE ID</b></th>
  3. <th><b>LAST NAME</b></th>
  4. <th><b>SALARY</b></th>
  5. <th><b>BONUS</b></th>
  6. </tr>
  7. <xsl:for-each select="EMPLOYEES">
  8. <xsl:for-each select="EMPLOYEE">
  9. <td><xsl:value-of select="@id"/></td>
  10. <td><xsl:value-of select="ENAME"/></td>
  11. <td><xsl:value-of select="SALARY"/></td>
  12. <td><xsl:value-of select="SALARY*0.1"/></td>
  13. </tr>
  14. </xsl:for-each>
  15. </xsl:for-each>
  16. </table>
 

You might also want to calculate the average salary for the employees included in the employees XML document. To achieve this, you might further modify the employees.xsl stylesheet by adding the following XSLT construction immediately after the code shown above:

  1. <p><b>Average salary is: </b><xsl:value-of
  2.       select="format-number(sum(//SALARY) div
  3.       count(//EMPLOYEE), '#######0.00')"/>
 

In this example, you sum the salaries of all employees included in the document with the sum function, and then divide the calculated sum by the number of employees obtained with the count function, thus getting the average salary formatted with the format-number function.

NOTE: For more examples of XSLT stylesheets, you can refer to the W3C XSL Transformations (XSLT) Version 1.0 recommendation available at http://www.w3.org/TR/xslt.

Now that you have a grasp on how to create XSLT stylesheets to be used for transforming and processing XML data, it's time to see an XSL transformation in action.

The following listing contains a simple PHP script that performs an XSL transformation, applying the employees.xsl XSLT stylesheet defined earlier in this section to the employees XML document shown in the Creating XML with the DOM PHP Extension section. It is assumed that the employees.xsl, employees.xml, and the XSLTrans.php files reside in the same directory.

  1. <?php
  2.    //File: XSLTrans.php
  3.    $domxsl = new DOMDocument();
  4.    $domxsl->load('employees.xsl');
  5.    $proc = new XSLTProcessor;
  6.    $xsl = $proc->importStylesheet($domxsl);
  7.    $domxml = new DOMDocument();
  8.    $domxml->load('employees.xml');
  9.    $rslt = $proc->transformToXml($domxml);
  10.    print $rslt;
  11. ?>
 

After you have created a new DOM document, you load the XSL stylesheet discussed earlier in this section into that document. Next, you create a new XSLTProcessor object that is then used to perform an XSL transformation. However, before you can do this, you need to import the stylesheet into the newly created XSLT processor, and you also need to create a new DOM document and then load the XML document to be transformed.

NOTE: For simplicity, in this example you do not query the database, nor do you generate a new XML document from scratch with the DOM functions. Instead, you load the existing document employees.xml, which was generated and saved to disk during the execution of the DOM.php script discussed in the Creating XML with the DOM PHP Extension section earlier in this chapter.

The XSL transformation performed in the above script transforms the employees XML document into an HTML page that you then send to the user's browser.

When you run the XSLTrans.php script defined in the above listing, the result should be something like the previous figure.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape