15.10.XMLType

XMLType is a predefined type for representing XML entities as UDT objects. This is compatible with Oracle9i and later. You can get better performance and flexibility by using use plain built-in functions that directly operate with XML entities. All predefined member functions of XMLType for extracting fragments from an XMLType are actually wrappers for xpath_eval() built-in function. You can declare a column of XMLType but the actual type of the created column will be "LONG XML". Thus any XML-related feature that can be used for "LONG XML" column will work with XMLType. E.g. WITH SCHEMA constraint allows you to force stored values to match a particular schema; XML free text index can accelerate search for documents by content etc.

XMLType behaves like any user-defined type, with the only difference in type conversion rules. If an XML entity is passed as an argument instead of an instance of XMLType, a new instance of XMLType is created by a constructor that takes the entity as an argument. Similarly, functions that accept XML entities as arguments can also accept an instance of XMLType as an actual value of argument.

The following example creates a table with an XMLType column, put two records there and performs a simple search:

CREATE TABLE Xml_tab ( xmlval XMLType);

INSERT INTO Xml_tab VALUES (
   xmltype('<?xml version="1.0"?>
               <EMP>
                  <EMPNO>221</EMPNO>
                  <ENAME>John</ENAME>
               </EMP>'));

INSERT INTO Xml_tab VALUES (
   xmltype('<?xml version="1.0"?>
               <PO>
                  <PONO>331</PONO>
                  <PONAME>PO_1</PONAME>
               </PO>'));

-- now extract the numerical values for the employee numbers

SELECT e.xmlval.extract('//EMPNO/text()').getNumVal() as empno
   FROM Xml_tab
   WHERE e.xmlval.existsnode('/EMP/EMPNO')  = 1;

To create a new instance of XMLType, the constructor XMLType() or a function createXML() is used.

Virtuoso can perform XPATH search in XMLType instances: extract() and existsNode() member functions are convenient for simple searches and any built-in XPATH functions like xpath_eval() or xquery_eval() can handle XMLType parameters instead of XML entity parameters. An application can use getStringVal() , getNumVal() and getColbVal() member functions to convert found node to strings, numbers or an XML source text of the node.

Instances of the type can store the URL of the XML schema to which they should conform. This URL can be specified when an instance is constructed; Once an instance is created, its schema URL cannot be changed but a modified copy can be created by createSchemaBasedXML() and createNonSchemaBasedXML() member functions; isSchemaBased() and getSchemaURL() member functions check whether the given instance is schema based or not and what particular schema is used.

An schema based XMLType instance can be validated against its schema; If it has been validated against its schema once with no errors detected then a special "VALIDATED" flag is set in the instance indicating that there is no need to validate it again. schemaValidate() member function performs the validation, isSchemaValidated() queries the "VALIDATED" flag and setSchemaValidated() changes the "VALIDATED" flag if application needs optimization tricks.

In addition, any instance can be validated against an arbitrary schema via member function isSchemaValid() . Built-in function xml_validate_schema() may accept instance of XMLType as its first argument, providing even more validation functionality.