alt
Advertisement

Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow PHP Oracle arrow Using XMLType Views
Site Search
Sponsored Links



Using XMLType Views
Article Index
Using XMLType Views
Creating XML Schema-Based XMLType Views

Using XMLType Views

Using XMLType Views XMLType views provide a convenient way to construct XML representations of relational data without physically migrating that data into XML. Once written, an XMLType view may be used in various queries, making them simpler and so increasing their readability.

Turning back to the SELECT statement used in the SQLXMLQuery.php script discussed in the Using Oracle SQL/XML Generation Functions section earlier in this chapter, you might create an XMLType view based on that statement as shown below.

  1. CONN /AS sysdba
  2.    GRANT CREATE ANY VIEW TO xmlusr;
  3.    CONN xmlusr/xmlusr;
  4.    CREATE VIEW EmpsXML AS
  5.       SELECT XMLELEMENT("EMPLOYEES",
  6.          XMLAgg(
  7.          XMLELEMENT("EMPLOYEE",
  8.          XMLATTRIBUTES(employee_id AS "id"),
  9.          XMLFOREST(last_name AS "ENAME", salary AS "SALARY")
  10.          )
  11.        )
  12.     ) AS xmlcol,
  13.     department_id AS dept_id
  14.    FROM hr.employees GROUP BY department_id;
 

In this example, you start by granting the CREATE VIEW privilege to the xmlusr database schema and then, when connected as xmlusr/xmlusr, create the EmpsXML view based on the query that uses SQL/XML functions to generate XML from the data stored in the hr.employees relational table.

The good thing about the EmpsXML view is that it hides the details of generatingan employees XML document, thus letting you write simpler and more readable queries. With it, the query used in the SQLXMLQuery.php script might be rewritten as follows:

$query = 'SELECT xmlcol as RESULT FROM EmpsXML WHERE
	dept_id=:deptid';

Before running the updated SQLXMLQuery.php script, make sure to specify the xmlusr/xmlusr schema in the oci_connect function at the beginning of the script as follows:


$rsConnection = oci_connect('xmlusr', 'xmlusr', '//localhost/orcl'))

Also, you might rewrite the query string used in the DBServerXSLTrans.php script discussed in the Performing XSLT Transformations inside the Database section earlier in this chapter as follows:

$query = 'SELECT XMLtransform(x.xmlcol,
  (SELECT stylesheet FROM XSLTstylesheets
  WHERE id = 1)).getStringVal()
  AS result FROM
  (SELECT * FROM EmpsXML WHERE dept_id=:deptid) x';

As you can see, the above query is three times smaller than the one originally used in the DBServerXSLTrans.php script.



 
< 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