February 2012
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
272829  

SQL/XML and XPath Rewrite

SQL/XML provides a series of functions to integrate XML functionality to SQL processing,below is the list of such functions:

APPENDCHILDXML
DELETEXML
DEPTH
EXTRACT (XML)
EXISTSNODE
EXTRACTVALUE
INSERTCHILDXML
INSERTXMLBEFORE
PATH
SYS_DBURIGEN
SYS_XMLAGG
SYS_XMLGEN
UPDATEXML
XMLAGG
XMLCDATA
XMLCOLATTVAL
XMLCOMMENT
XMLCONCAT
XMLFOREST
XMLPARSE
XMLPI
XMLQUERY
XMLROOT
XMLSEQUENCE
XMLSERIALIZE
XMLTABLE
XMLTRANSFORM

Also, XMLType is important in Oracle XML DB, which I have discussed in the last post.

XMLType is a system-defined opaque type for handling XML data. It has predefined member functions on it to extract XML nodes and fragments.

You can create columns of XMLType and insert XML documents into it. You can also generate XML documents as XMLType instances dynamically using the SYS_XMLGEN and SYS_XMLAGG SQL functions.

Table 198-1 summarizes functions and procedures of the XMLType.

Table 198-1 XMLTYPE Subprograms

Method Description

CREATENONSCHEMABASEDXML

Creates a non schema based XML from the input schema based instance.

CREATESCHEMABASEDXML

Creates a schema based XMLType instance from the non-schema based instance using the input schema URL.

CREATEXML

Static function for creating and returning an XMLType instance.

EXISTSNODE

Takes a XMLType instance and a XPath and returns 1 or 0 indicating if applying the XPath returns a non-empty set of nodes.

EXTRACT

Takes a XMLType instance and an XPath, applies the XPath expression and returns the results as an XMLType.

GETBLOBVAL

Returns the value of the XMLType instance as a BLOB

GETCLOBVAL

Returns the value of the XMLType instance as a CLOB.

GETNAMESPACE

Returns the namespace for the top level element in a schema based document.

GETNUMBERVAL

Returns the value of the XMLType instance as a NUMBER. This is only valid if the input XMLType instance contains a simple text node and is convertible to a number.

GETROOTELEMENT

Returns the root element of the input instance. Returns NULL if the instance is a fragment

GETSCHEMAURL

Returns the XML schema URL if the input is an XML Schema based.

GETSTRINGVAL

Returns the value of the XMLType instance as a string.

ISFRAGMENT

Checks if the input XMLType instance is a fragment or not. A fragment is a XML instance, which has more than one root element.

ISSCHEMABASED

Returns 1 or 0 indicating if the input XMLType instance is a schema based one or not.

ISSCHEMAVALID

Checks if the input instance is schema valid according to the given schema URL.

ISSCHEMAVALIDATED

Checks if the instance has been validated against the schema.

SCHEMAVALIDATE

Validates the input instance according to the XML Schema. Raises error if the input instance is non-schema based.

SETSCHEMAVALIDATED

Sets the schema valid flag to avoid costly schema validation.

TOOBJECT

Converts the XMLType instance to an object type.

TRANSFORM

Takes an XMLType instance and an associated stylesheet (which is also an XMLType instance), applies the stylesheet and returns the result as XML.

XMLTYPE

Constructs an instance of the XMLType datatype. The constructor can take in the XML as a CLOB, VARCHAR2 or take in a object type.



The SQL/XML SQL functions and their corresponding XMLType methods allow XPath expressions to be used to search collections of XML documents and to access a subset of the nodes contained within an XML document.

Oracle XML DB provides two ways of evaluating XPath expressions that operate on XMLType columns and tables, depending on the XML storage method used:

  1. Structured-storage XML data: Oracle XML DB attempts to translate the XPath expression in a SQL/XML function into an equivalent SQL query. The SQL query references the object-relational data structures that underpin a schema-based XMLType. This process is referred to as XPath rewrite. It can occur when performing queries and UPDATE operations.
  2. Unstructured-storage XML data: Oracle XML DB evaluates the XPath expression using functional evaluation. Functional evaluation builds a DOM tree for each XML document, and then resolves the XPath programmatically using the methods provided by the DOM API. If the operation involves updating the DOM tree, the entire XML document has to be written back to disc when the operation is completed.

One example of XPath Rewrite is as following:

SELECT OBJECT_VALUE FROM mypurchaseorders p
WHERE extractValue(OBJECT_VALUE, '/PurchaseOrder/Company') = 'Oracle';

will become:

SELECT VALUE(p) FROM mypurchaseorders p WHERE p.xmldata.Company = 'Oracle';

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>