PHP Oracle Tutorials
Tutorials
PHP OraclePerforming 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.
- <?php
- //File: DBServerXSLTrans.php
- if(!$rsConnection = oci_connect('xmlusr', 'xmlusr',
- '//localhost/orcl')) {
- $err = oci_error();
- '.$err['message'], E_USER_ERROR);
- };
- $dept_id = 90;
- $query = 'SELECT XMLtransform(x.xmlcol,
- (SELECT stylesheet FROM XSLTstylesheets WHERE
- id = 1)).getStringVal()
- AS result FROM
- (SELECT XMLELEMENT("EMPLOYEES",
- XMLAgg(
- XMLELEMENT("EMPLOYEE",
- XMLATTRIBUTES(employee_id AS "id"),
- XMLFOREST(last_name AS "ENAME", salary AS "SALARY")
- )
- )
- ) AS xmlcol
- FROM hr.employees WHERE department_id=:deptid) x';
- $stmt = oci_parse($rsConnection,$query);
- oci_bind_by_name($stmt, ':deptid', $dept_id);
- if (!oci_execute($stmt)) {
- $err = oci_error($stmt);
- }
- $xmlDoc = oci_fetch_assoc($stmt);
- $dom = new DOMDocument();
- $dom->loadXML($xmlDoc['RESULT']);
- ?>
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/" target="_blank" rel="nofollow", 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.
