Exforsys.com
 
Home Tutorials PHP Oracle
 

Using XMLType for Handling XML Data in the Database

 

Using XMLType for Handling XML Data in the Database

Being an object type, XMLType can not only be used to store XML data in the database but also to operate on that data via its built-in methods. Regardless of the storage model you choose, XMLType provides a set of XML-specific methods to operate on XMLType instances.



The most commonly used methods of XMLType are listed in the following table:


XMLType method Description
existsNode Checks whether the XML document in a given XMLType instance contains a node that matches the XPath expression passed as the parameter. If the specifi ed node is found, it returns 1; otherwise, it returns 0.
extract Extracts a node or nodes from the XML document in an XMLType instance, based on the XPath expression passed as the parameter. Returns the result nodes as an XMLType instance.
createSchemaBasedXML Explicitly associates an XML document in an XMLType instance with a registered XML schema specifi ed in the parameter. You might want to perform this operation when inserting an XML document into an XML schema-based XMLType column or table.
schemaValidate Validates an XML document in an XMLType instance against an XML schema specifi ed in the parameter. On success, the status of the document is changed to VALIDATED; otherwise, an error is raised.
transform Transforms an XML document in an XMLType instance with the XSL stylesheet specifi ed in the parameter. Returns the resultant document as an XMLType instance.

You saw an example of using an XMLType method in the preceding sample application. In particular, in the DBServerXSLTrans.php script discussed in the Performing XSLT Transformations inside the Database section you use the getStringVal method of XMLType to retrieve the generated XHTML data as a VARCHAR value, so that it can then be loaded in a DOMDocument instance. If you recall, the query used in the DBServerXSLTrans.php script looks as follows:


Sample Code
  1.         $query = 'SELECT XMLtransform(x.xmlcol,
  2.         (SELECT stylesheet FROM XSLTstylesheets WHERE
  3.                 id = 1)).getStringVal()
  4.         AS result FROM
  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.         FROM hr.employees WHERE department_id=:deptid) x';
Copyright exforsys.com


To see another XMLType method in action, namely transform, you might rewritethe above SQL statement as follows:


Sample Code
  1.         $query = 'SELECT x.xmlcol.transform((SELECT stylesheet FROM
  2.         XSLTstylesheets WHERE id = 1)).getStringVal()
  3.         AS result FROM
  4.         (SELECT XMLELEMENT("EMPLOYEES",
  5.                 XMLAgg(
  6.                 XMLELEMENT("EMPLOYEE",
  7.                 XMLATTRIBUTES(employee_id AS "id"),
  8.                 XMLFOREST(last_name AS "ENAME", salary AS "SALARY")
  9.                 )
  10.            )
  11.         ) AS xmlcol
  12.         FROM hr.employees WHERE department_id=:deptid) x';
Copyright exforsys.com


In the above query, you use the transform XMLType method as an alternative to the XMLtransform SQL function used in the original query. Since both transform and XMLtransform have the same functionality, the above queries will produce the same general result.


NOTE: XMLtransform is not the only example of an SQL function providing the same functionality as the appropriate XMLType method. In fact, Oracle XML DB provides analogous SQL functions for many XMLType methods. For example, XMLType methods extract and existsNode can be used instead of the SQL functions having the same names.



As you can see, the above queries operate on relational data, and transform it into XML format with SQL/XML generation functions. Before looking at the approaches you can take to retrieve XML data stored in the database natively, however, it would be a good idea to look at how you can create an XMLType storage in Oracle XML DB. The following section discusses how you can do this with the help of the XML Schema feature.



Read Next: Using XML Schemas



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape