Reviews
PHP Oracle Training TutorialsTable of Contents
Querying Data with Oracle XQuery
Using XQuery to Construct XML from Relational DataUsing XQuery to Construct XML from Relational Data
Querying Data with Oracle XQuery
Using XQuery to Construct XML from Relational Data
In the preceding sections, you saw several examples of how to construct XML representations of relational data using the SQL/XML generation function, as well as using object types when creating XMLType views on relational tables. In this section, you will learn how to build XML on relational data with XQuery.
Turning back to the Using Oracle SQL/XML Generation Functions section, you might modify the listing containing the SQLXMLQuery.php script that uses PHP DOM extension functions to produce an XML representation of relational data to use XQuery instead of those functions, as shown overleaf:
- <?php
- //File: XQuery.php
- if(!$rsConnection = oci_connect('hr', 'hr', '//localhost/orcl')) {
- $err = oci_error()
- trigger_error('Could not establish a connection: ' .
- $err['message'], E_USER_ERROR)
- }
- $dept_id=90
- $query =
- 'SELECT XMLQuery('.
- "'".'for $j in 1
- return (
- <EMPLOYEES> {
- for $i in ora:view("hr", "employees")/ROW
- where $i/DEPARTMENT_ID = $deptid
- return (<EMPLOYEE id="{xs:integer($i/EMPLOYEE_ID)}">
- <ENAME>{xs:string($i/LAST_NAME)}</ENAME>
- <SALARY>{xs:integer($i/SALARY)}</SALARY>
- </EMPLOYEE>)} </EMPLOYEES>)'."'".
- 'PASSING XMLElement("deptid", :deptid) AS "deptid"
- RETURNING CONTENT).getStringVal() RESULT FROM DUAL'
- $stmt = oci_parse($rsConnection,$query)
- oci_bind_by_name($stmt, ':deptid', $dept_id)
- if (!oci_execute($stmt)) {
- $err = oci_error($stmt)
- trigger_error('Query failed: ' . $err['message'], E_USER_ERROR)
- }
- $xmlDoc = oci_fetch_assoc($stmt)
- $domxml = new DOMDocument()
- $domxml->loadXML($xmlDoc['RESULT'])
- print $domxml->saveXML()
- ?>
The Oracle SQL function XMLQuery can be used to construct or query XML data, based on an XQuery expression passed as the parameter. In this example, you use the XMLQuery function to generate an XML representation of some data stored in the relational table hr.employees.
You start the FLWOR expression used in this example with the for clause performing only one iteration.
NOTE: FLWOR stands for for, let, where, order by, return?the clauses used when composing an XQuery expression.
Next, in the nested FLWOR expression that starts with the for clause, you iterate over the hr.employees rows selected based on the condition specifi ed in the where clause. With the help of the ora:view XQuery function, you query relational table hr.employees, as it were an XMLType table, creating XML documents on the fly.
In the return clause of the FLWOR expression, you construct the EMPLOYEE nodes of the resultant EMPLOYEES document.
You bind dynamic variable deptid to an XQuery expression using the PASSING clause. This variable is used in the where clause of the nested FLWOR expression, restricting the retrieved employee records to those that belong to the specified department.
When executed, the XQuery.php script shown in the listing opposite should produce the same XML document as the one shown in the Creating XML with the DOM PHP Extension section at the beginning of this chapter. If you want to print the resultant XML document in HTML format, you might perform an XSL transformation before outputting it. To achieve this, you might replace the last line of code in the script:
print $domxml->saveXML();
with the following lines:
- $domxsl = new DOMDocument();
- $domxsl->load('employees.xsl');
- $proc = new XSLTProcessor;
- $xsl = $proc->importStylesheet($domxsl);
- $rslt = $proc->transformToXml($domxml);
Assuming that you have created the employees.xsl stylesheet as discussed in the Transforming and Processing XML with XSLT section earlier in this chapter, with the above replacement, the XQuery.php script shown in the listing should produce the HTML table as shown in the Transforming and Processing XML with XSLT section earlier.
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







