Reviews
PHP Oracle Training TutorialsUsing 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.
- CONN /AS sysdba
- GRANT CREATE ANY VIEW TO xmlusr;
- CONN xmlusr/xmlusr;
- CREATE VIEW EmpsXML AS
- SELECT XMLELEMENT("EMPLOYEES",
- XMLAgg(
- XMLELEMENT("EMPLOYEE",
- XMLATTRIBUTES(employee_id AS "id"),
- XMLFOREST(last_name AS "ENAME", salary AS "SALARY")
- )
- )
- ) AS xmlcol,
- department_id AS dept_id
- 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.
PHP Oracle Training Tutorials
- PHP Oracle Web Development
- XML Processing in PHP and Oracle Applications
- Creating XML with the DOM PHP Extension
- Querying a DOM Document with XPath
- Transforming and Processing XML with XSLT
- Performing XML Processing inside the Database
- Moving All the XML Processing into the Database
- Performing XSLT Transformations inside the Database
- Using Oracle Database for Storing, Modifying, and Retrieving XML Data
- Using XMLType for Handling XML Data in the Database
- Using XML Schemas
- Retrieving XML Data
- Using XMLType Views
- Performing DML Operations on XML Schema Based XMLType Views
- Using Oracle XML DB Repository
- Accessing Repository Resources with SQL
- Taking Advantage of Standard Internet Protocols
- Querying Data with Oracle XQuery
- Breaking up XML into Relational Data







