alt
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow PHP Oracle arrow Performing XSLT Transformations inside the Database
Site Search


Performing XSLT Transformations inside the Database

Performing XSLT Transformations inside the Database

Now that you have the employees XSL stylesheet stored in the database and the xmlusr schema is permitted to access the hr.employees table, you can create a script that will instruct the database to build an HTML page based on the data stored in hr.employees.

The following listing contains the source code for such a script.

  1. <?php
  2.    //File: DBServerXSLTrans.php
  3.    if(!$rsConnection = oci_connect('xmlusr', 'xmlusr',
  4.             '//localhost/orcl')) {
  5.       $err = oci_error();
  6.       trigger_error('Could not establish a connection:
  7.       '.$err['message'], E_USER_ERROR);
  8.    };
  9.    $dept_id = 90;
  10.    $query = 'SELECT XMLtransform(x.xmlcol,
  11.    (SELECT stylesheet FROM XSLTstylesheets WHERE
  12.       id = 1)).getStringVal()
  13.    AS result FROM
  14.    (SELECT XMLELEMENT("EMPLOYEES",
  15.       XMLAgg(
  16.       XMLELEMENT("EMPLOYEE",
  17.       XMLATTRIBUTES(employee_id AS "id"),
  18.       XMLFOREST(last_name AS "ENAME", salary AS "SALARY")
  19.     )
  20.     )
  21.    ) AS xmlcol
  22.    FROM hr.employees WHERE department_id=:deptid) x';
  23.    $stmt = oci_parse($rsConnection,$query);
  24.    oci_bind_by_name($stmt, ':deptid', $dept_id);
  25.    if (!oci_execute($stmt)) {
  26.       $err = oci_error($stmt);
  27.       trigger_error('Query failed: '.$err['message'], E_USER_ERROR);
  28.    }   
  29.    $xmlDoc = oci_fetch_assoc($stmt);
  30.    $dom = new DOMDocument();
  31.    $dom->loadXML($xmlDoc['RESULT']);
  32.    echo $dom->saveXML();
  33.    ?>
 

As you can see, the select list of the SELECT statement used in the DBServerXSLTrans.php script includes the XMLtransform SQL/XML function. This function is used here to apply the employees XSL stylesheet retrieved from the XSLTstylesheets table by the subquery to the employees XML document generated by the subquery defined in the FROM clause of the query. The result of this transformation should be an HTML page, which you load into a new DOMDocument object and then display it in the browser. When displayed, the generated HTML page should look like the figure shown in the Transforming and Processing XML with XSLT section shown earlier in this chapter.

Building PHP Applications on Oracle XML DB

The preceding example shows how you might move the XML processing performed by your PHP/Oracle application from PHP to Oracle, thus taking advantage of the optimizations provided by the Oracle database server. In particular, you saw how to generate an XML document from scratch and apply an XSL transformation inside the database, rather than performing these operations with PHP.

In fact, Oracle XML Database provides much more functionality than what the sample demonstrates.

NOTE: Oracle XML DB refers to the set of Oracle Database XML technologies integrated with the relational database server, providing high performance XML storage, retrieval, and processing.

The most significant features of Oracle XML DB, which make Oracle database ideal for XML-enabled database-driven applications, are listed below:

  • Ability to store, retrieve, update, and transform XML data through the SQL and PL/SQL interfaces.
  • Ability to perform XML operations on SQL data without physically migrating it into XML format.
  • Oracle XML DB repository lets you manipulate XML content stored in the database with the standard Internet protocols, such as FTP, HTTP, and WebDAV.
  • Support for the Worldwide Web Consortium (W3C) XML Schema Recommendation: http://www.w3.org/TR/xmlschema-0/, allowing you to validate XML documents against appropriate XML schemas registered in the database.
  • XML-specific optimizations, reducing the cost of performing XML processing inside the database.

The subsections that follow show how you can make use of these features when building XML-enabled PHP/Oracle applications.


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