| Oracle9i XML Database Developer's Guide - Oracle XML DB Release 2 (9.2) Part Number A96620-02 |
|
|
View PDF |
This chapter introduces XML Schema and explains how XML schema are used in Oracle XML DB applications. It describes how to register your XML schema and create storage structures for storing schema-based XML. It explains in detail the mapping from XML to SQL storage types, including techniques for maintaining the DOM fidelity of XML data.This chapter also describes how queries over XMLType tables and columns based on this mapping are optimized using query rewrite techniques. It discusses the mechanism for generating XML schemas from existing object types.
This chapter contains the following sections:
The XML Schema Recommendation was created by the World Wide Web Consortium (W3C) to describe the content and structure of XML documents in XML. It includes the full capabilities of Document Type Definitions (DTDs) so that existing DTDs can be converted to XML schema. XML schemas have additional capabilities compared to DTDs.
XML Schema is a schema definition language written in XML. It can be used to describe the structure and various other semantics of conforming instance documents. For example, the following XML schema definition, po.xsd, describes the structure and other properties of purchase order XML documents.
This manual refers to an XML schema definition as an XML schema.
The following is an example of an XML schema definition, po.xsd:
<schema targetNamespace="http://www.oracle.com/PO.xsd" xmlns:po="http://www.oracle.com/PO.xsd" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType name="PurchaseOrderType"> <sequence> <element name="PONum" type="decimal"/> <element name="Company"> <simpleType> <restriction base="string"> <maxLength value="100"/> </restriction> </simpleType> </element> <element name="Item" maxOccurs="1000"> <complexType> <sequence> <element name="Part"> <simpleType> <restriction base="string"> <maxLength value="1000"/> </restriction> </simpleType> </element> <element name="Price" type="float"/> </sequence> </complexType> </element> </sequence> </complexType> <element name="PurchaseOrder" type="po:PurchaseOrderType"/> </schema>
The following is an example of an XML document that conforms to XML schema po.xsd:
<PurchaseOrder xmlns="http://www.oracle.com/PO.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/PO.xsd http://www.oracle.com/PO.xsd"> <PONum>1001</PONum> <Company>Oracle Corp</Company> <Item> <Part>9i Doc Set</Part> <Price>2550</Price> </Item> </PurchaseOrder>
Oracle XML DB uses annotated XML schema as metadata, that is, the standard XML Schema definitions along with several Oracle XML DB-defined attributes. These attributes are in a different namespace and control how instance documents get mapped into the database. Since these attributes are in a different namespace from the XML schema namespace, such annotated XML schemas are still legal XML schema documents:
| See Also:
Namespace of XML Schema constructs: |
When using Oracle XML DB, you must first register your XML schema. You can then use the XML schema URLs while creating XMLType tables, columns, and views.
Oracle XML DB provides XML Schema support for the following tasks:
XMLType tables, views and columns based on registered XML schemas.XMLType tables.As described in Chapter 4, "Using XMLType", XMLType is a datatype that facilitates storing XML in columns and tables in the database. XML schemas further facilitate storing XML columns and tables in the database, and they offer you more storage and access options for XML data along with space- performance-saving options.
For example, you can use XML schema to declare which elements and attributes can be used and what kinds of element nesting, and datatypes are allowed in the XML documents being stored or processed.
Using XML schema with Oracle XML DB provides a flexible setup for XML storage mapping. For example:
Which storage method you choose depends on how your data will be used and depends on the queriability and your requirements for querying and updating your data. In other words. Using XML schema gives you more flexibility for storing highly structured or unstructured data.
Another advantage of using XML schema with Oracle XML DB is that you can perform XML instance validation according to the XML schema and with respect to Oracle XML Repository requirements for optimal performance. For example, an XML schema can check that all incoming XML documents comply with definitions declared in the XML schema, such as allowed structure, type, number of allowed item occurrences, or allowed length of items.
Also, by registering XML schema in Oracle XML DB, when inserting and storing XML instances using Protocols, such as FTP or HTTP, the XML schema information can influence how efficiently XML instances are inserted.
When XML instances must be handled without any prior information about them, XML schema can be useful in predicting optimum storage, fidelity, and access.
In addition to supporting XML schema that provide a structured mapping to object- relational storage, Oracle XML DB also supports DTD specifications in XML instance documents. Though DTDs are not used to derive the mapping, XML processors can still access and interpret the DTDs.
When an XML instance document has an inline DTD definition, it is used during document parsing. Any DTD validations and entity declaration handling is done at this point. However, once parsed, the entity references are replaced with actual values and the original entity reference is lost.
Oracle XML DB also supports external DTD definitions if they are stored in the Repository. Applications needing to process an XML document containing an external DTD definition such as "/public/flights.dtd", must first ensure that the DTD document is stored in Oracle XML DB at the path "/public/flights.xsd".
Oracle XML DB's XML schema functionality is available through the PL/SQL supplied package, DBMS_XMLSCHEMA, a server-side component that handles the registration of XML schema definitions for use by Oracle XML DB applications.
Two of the main DBMS_XMLSCHEMA functions are:
. This deletes a previously registered XML schema, identified by its URL or XMLSchema name.An XML schema must be registered before it can be used or referenced in any context by Oracle XML DB. XML schema are registered by using DBMS_XMLSCHEMA.registerSchema() and specifying the following:
XMLType, or URIType.After registration has completed:
Use DBMS_XMLSCHEMA to register your XML schema. This involves specifying the XML schema document and its URL, also known as the XML schema location.
Consider the following XML schema. It declares a complexType called PurchaseOrderType and an element PurchaseOrder of this type. The schema is stored in the PL/SQL variable doc. The following registers the XML schema at URL: http://www.oracle.com/PO.xsd:
declare doc varchar2(1000) := '<schema targetNamespace="http://www.oracle.com/PO.xsd" xmlns:po="http://www.oracle.com/PO.xsd" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType name="PurchaseOrderType"> <sequence> <element name="PONum" type="decimal"/> <element name="Company"> <simpleType> <restriction base="string"> <maxLength value="100"/> </restriction> </simpleType> </element> <element name="Item" maxOccurs="1000"> <complexType> <sequence> <element name="Part"> <simpleType> <restriction base="string"> <maxLength value="1000"/> </restriction> </simpleType> </element> <element name="Price" type="float"/> </sequence> </complexType> </element> </sequence> </complexType> <element name="PurchaseOrder" type="po:PurchaseOrderType"/> </schema>'; begin dbms_xmlschema.registerSchema('http://www.oracle.com/PO.xsd', doc); end;
The registered schema can be used to created XML schema-Based tables, or XML schema-based columns. For example, the following statement creates an a table with an XML schema-based column.
create table po_tab( id number, po sys.XMLType ) xmltype column po XMLSCHEMA "http://www.oracle.com/PO.xsd" element "PurchaseOrder";
The following shows an XMLType instance that conforms to the preceding XML schema being inserted into the preceding table. The schemaLocation attribute specifies the schema URL:
insert into po_tab values (1, xmltype('<po:PurchaseOrder xmlns:po="http://www.oracle.com/PO.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/PO.xsd http://www.oracle.com/PO.xsd"> <PONum>1001</PONum> <Company>Oracle Corp</Company> <Item> <Part>9i Doc Set</Part> <Price>2550</Price> </Item> <Item> <Part>8i Doc Set</Part> <Price>350</Price> </Item> </po:PurchaseOrder>'));
XML schemas can be registered as local or global:
When you register an XML schema, DBMS_XMLSCHEMA adds an Oracle XML DB resource corresponding to the XML schema into the Oracle XML DB Repository. The XML schema URL determines the path name of the resource in Oracle XML DB Repository according to the following rules:
In Oracle XML DB, local XML schema resources are created under the /sys/schemas/<username> directory. The rest of the path name is derived from the schema URL.
For example, a local XML schema with schema URL:
http://www.myco.com/PO.xsd
registered by SCOTT, is given the path name:
/sys/schemas/SCOTT/www.myco.com/PO.xsd.
Database users need appropriate permissions (ACLs) to create a resource with this path name in order to register the XML schema as a local XML schema.
By default, an XML schema belongs to you after registering the XML schema with Oracle XML DB. A reference to the XML schema document is stored in Oracle XML DB Repository, in directory:
/sys/schemas/<username>/....
For example, if you, SCOTT, registered the preceding XML schema, it is mapped to the file:
/sys/schemas/SCOTT/www.oracle.com/PO.xsd
Such XML schemas are referred to as local. In general, they are usable only by you to whom they belong.
In contrast to local schema, privileged users can register an XML schema as a global XML schema by specifying an argument in the DBMS_XMLSCHEMA registration function.
Global schemas are visible to all users and stored under the /sys/schemas/PUBLIC/ directory in Oracle XML DB Repository.
|
Note: Access to this directory is controlled by Access Control Lists (ACLs) and, by default, is writable only by a DBA. You need WRITE privileges on this directory to register global schemas.
See also Chapter 18, "Oracle XML DB Resource Security" for further information on privileges and for details on XDBAdmin role. |
You can register a local schema with the same URL as an existing global schema. A local schema always hides any global schema with the same name (URL).
For example, a global schema registered by SCOTT with the URL:
www.myco.com/PO.xsd
is mapped to Oracle XML DB Repository at:
/sys/schemas/PUBLIC/www.myco.com/PO.xsd
Database users need appropriate permissions (ACLs) to create this resource in order to register the XML schema as global.
As part of registering an XML schema, Oracle XML DB also performs several other steps to facilitate storing, accessing, and manipulating XML instances that conform to the XML schema. These steps include:
XMLType tables for all root elements. You can also specify any column and table level constraints for use during table creation.
You can delete your registered XML schema by using the DBMS_XMLSCHEMA.deleteSchema procedure. When you attempt to delete an XML schema, DBMS_XMLSCHEMA checks:
A FORCE mode option is provided while deleting XML schemas. If you specify the FORCE mode option, the XML schema deletion proceeds even if it fails the dependency check. In this mode, XML schema deletion marks all its dependents as invalid.
The CASCADE mode option drops all generated types and default tables as part of a previous call to register XML schema.
| See Also:
Oracle9i XML API Reference - XDK and Oracle XML DB the chapter on |
The following example deletes XML schema PO.xsd. First, the dependent table po_tab is dropped. Then, the schema is deleted using the FORCE and CASCADE modes with DBMS_XMLSCHEMA.DELETESCHEMA:
drop table po_tab; EXEC dbms_xmlschema.deleteSchema('http://www.oracle.com/PO.xsd', dbms_xmlschema.DELETE_CASCADE_FORCE);
The following sections describe guidelines for registering XML schema with Oracle XML DB.
The following objects depend on a registered XML schemas:
XMLType column that conforms to some element in the XML schema.After an XML schema has been registered, it can be used to create XML schema-based XMLType tables, views, and columns by referencing the following:
For example you can create an XML schema-based XMLType table as follows:
CREATE TABLE po_tab OF XMLTYPE XMLSCHEMA "http://www.oracle.com/PO.xsd" ELEMENT "PurchaseOrder";
The following statement inserts XML schema-conformant data:
insert into po_tab values ( xmltype('<PurchaseOrder xmlns="http://www.oracle.com/PO.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/PO.xsd http://www.oracle.com/PO.xsd"> <PONum>1001</PONum> <Company>Oracle Corp</Company> <Item> <Part>9i Doc Set</Part> <Price>2550</Price> </Item> <Item> <Part>8i Doc Set</Part> <Price>350</Price> </Item> </PurchaseOrder>'));
You can validate an XMLType instance against a registered XML schema by using one of the validation methods.
The following PL/SQL example validates an XML instance against XML schema PO.xsd:
declare xmldoc xmltype; begin -- populate xmldoc (by fetching from table) select value(p) into xmldoc from po_tab p; -- validate against XML schema xmldoc.schemavalidate(); if xmldoc.isschemavalidated() = 1 then dbms_output.put_line('Data is valid'); else dbms_output.put_line('Data is invalid'); end if; end;
By default, XML schema URL names are always referenced within the scope of the current user. In other words, when database users specify XML Schema URLs, they are first resolved as the names of local XML schemas owned by the current user.
These rules imply that, by default, users cannot reference the following kinds of XML schemas:
To permit explicit reference to XML schemas in these cases, Oracle XML DB supports a notion of fully qualified XML schema URLs. In this form, the name of the database user owning the XML schema is also specified as part of the XML schema URL, except that such XML schema URLs belong to the Oracle XML DB namespace as follows: http://xmlns.oracle.com/xdb/schemas/<database-user-name>/<schemaURL-minus-protocol>
For example, consider the global XML schema with the following URL: http://www.example.com/po.xsd
Assume that database user SCOTT has a local XML schema with the same URL:
http://www.example.com/po.xsd
User JOE can reference the local XML schema owned by SCOTT as follows:
http://xmlns.oracle.com/xdb/schemas/SCOTT/www.example.com/po.xsd
Similarly, the fully qualified URL for the global XML schema is:
http://xmlns.oracle.com/xdb/schemas/PUBLIC/www.example.com/po.xsd
Registration of an XML schema is non transactional and auto committed as with other SQL DDL operations, as follows:
Since XML schema registration potentially involves creating object types and tables, error recovery involves dropping any such created types and tables. Thus, the entire XML schema registration is guaranteed to be atomic. That is, either it succeeds or the database is restored to the state before the start of registration.
An XML schema can be generated from an object-relational type automatically using a default mapping. The generateSchema() and generateSchemas() functions in the DBMS_XMLSCHEMA package take in a string that has the object type name and another that has the Oracle XML DB XML schema.
generateSchema() returns an XMLType containing an XML schema. It can optionally generate XML schema for all types referenced by the given object type or restricted only to the top-level types.generateSchemas() is similar, except that it returns an XMLSequenceType of XML schemas, each corresponding to a different namespace. It also takes an additional optional argument, specifying the root URL of the preferred XML schema location:
http://xmlns.oracle.com/xdb/schemas/<schema>.xsd
They can also optionally generate annotated XML schemas that can be used to register the XML schema with Oracle XML DB.
For example, given the object type:
connect t1/t1 CREATE TYPE employee_t AS OBJECT ( empno NUMBER(10), ename VARCHAR2(200), salary NUMBER(10,2) );
You can generate the schema for this type as follows:
select dbms_xmlschema.generateschema('T1', 'EMPLOYEE_T') from dual;
This returns a schema corresponding to the type EMPLOYEE_T. The schema declares an element named EMPLOYEE_T and a complexType called EMPLOYEE_TType. The schema includes other annotation from http://xmlns.oracle.com/xdb.
DBMS_XMLSCHEMA.GENERATESCHEMA('T1','EMPLOYEE_T') -------------------------------------------------------------------------------- <xsd:schema targetNamespace="http://ns.oracle.com/xdb/T1" xmlns="http://ns.oracl e.com/xdb/T1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xml ns.oracle.com/xdb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sch emaLocation="http://xmlns.oracle.com/xdb http://xmlns.oracle.com/xdb/XDBSchema.x sd"> <xsd:element name="EMPLOYEE_T" type="EMPLOYEE_TType" xdb:SQLType="EMPLOYEE_T" xdb:SQLSchema="T1"/> <xsd:complexType name="EMPLOYEE_TType"> <xsd:sequence> <xsd:element name="EMPNO" type="xsd:double" xdb:SQLName="EMPNO" xdb:SQLTyp e="NUMBER"/> <xsd:element name="ENAME" type="xsd:string" xdb:SQLName="ENAME" xdb:SQLTyp e="VARCHAR2"/> <xsd:element name="SALARY" type="xsd:double" xdb:SQLName="SALARY" xdb:SQLT ype="NUMBER"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Table 5-1 lists the XMLType API's XML schema-related methods.
| XMLType API Method | Description |
|---|---|
|
isSchemaBased() |
Returns TRUE if the |
|
getNamespace() |
Returns the XML schema URL, name of root element, and the namespace for an XML schema-based |
|
setSchemaValidated() |
An |
XML schema documents are themselves stored in Oracle XML DB as XMLType instances. XML schema-related XMLType types and tables are created as part of the Oracle XML DB installation script, catxdbs.sql.
The XML schema for XML schemas is called the root XML schema, XDBSchema.xsd. XDBSchema.xsd describes any valid XML schema document that can be registered by Oracle XML DB. You can access XDBSchema.xsd through Oracle XML DB Repository at:
/sys/schemas/PUBLIC/xmlns.oracle.com/xdb/XDBSchema.xsd
XML Schema-based XMLType structures are stored in one of the following ways:
STORE AS clause of the CREATE TABLE statement:
CREATE TABLE po_tab OF xmltype STORE AS CLOB ELEMENT "http://www.oracle.com/PO.xsd#PurchaseOrder";
Design criteria for storing XML data are discussed inChapter 2, "Getting Started with Oracle XML DB" and Chapter 3, "Using Oracle XML DB".
Instead of using the STORE AS clause, you can specify that the table and column be stored according to a mapping based on a particular XML schema. You can specify the URL for the XML schema used for the mapping.
Non-schema-based XML data can be stored in tables using CLOBs. However you do not gain benefits such as indexing, query-rewrite, and so on.
Document Object Model (DOM) fidelity is the concept of retaining the structure of a retrieved XML document, compared to the original XML document, for DOM traversals. DOM fidelity is needed to ensure the accuracy and integrity of XML documents stored in Oracle XML DB.
All elements and attributes declared in the XML schema are mapped to separate attributes in the corresponding SQL object type. However, some pieces of information in XML instance documents are not represented directly by these element or attributes, such as:
To ensure the integrity and accuracy of this data, for example, when regenerating XML documents stored in the database, Oracle XML DB uses a data integrity mechanism called DOM fidelity.
DOM fidelity refers to how identical the returned XML documents are compared to the original XML documents, particularly for purposes of DOM traversals.
To guarantee that DOM fidelity is maintained and that the returned XML documents are identical to the original XML document for DOM traversals, Oracle XML DB adds a system binary attribute, SYS_XDBPD$, to each created object type.
This positional descriptor attribute stores all pieces of information that cannot be stored in any of the other attributes, thereby ensuring the DOM fidelity of all XML documents stored in Oracle XML DB. Examples of such pieces of information include: ordering information, comments, processing instructions, namespace prefixes, and so on. This is mapped to a Positional Descriptor (PD) column.
|
Note: The PD attribute is mainly intended for Oracle internal use only. You should never directly access or manipulate this column. |
If DOM fidelity is not required, you can suppress SYS_XDBPD$ in the XML schema definition by setting the attribute, maintainDOM=FALSE.
Oracle XML DB creates XML schema-based XMLType tables and columns by referencing:
Figure 5-1 shows the syntax for creating an XMLType table:
CREATE TABLE [schema.] table OF XMLTYPE [XMLTYPE XMLType_storage] [XMLSchema_spec];

A subset of the XPointer notation, shown in the following example, can also be used to provide a single URL containing the XML schema location and element name.
This example creates the XMLType table po_tab using the XML schema at the given URL:
CREATE TABLE po_tab OF XMLTYPE XMLSCHEMA "http://www.oracle.com/PO.xsd" ELEMENT "PurchaseOrder";
An equivalent definition is:
CREATE TABLE po_tab OF XMLTYPE ELEMENT "http://www.oracle.com/PO.xsd#PurchaseOrder";
When an XML schema is registered, Oracle XML DB creates the appropriate SQL object types that enable structured storage of XML documents that conform to this XML schema. All SQL object types are created based on the current registered XML schema, by default.
or example, when PO.xsd is registered with Oracle XML DB, the following SQL types are created.
|
Note: The names of the types are generated names, and will not necessarily match |
CREATE TYPE "Itemxxx_T" as object ( part varchar2(1000), price number ); CREATE TYPE "Itemxxx_COLL" AS varray(1000) OF "Item_T"; CREATE TYPE "PurchaseOrderTypexxx_T" AS OBJECT ( ponum number, company varchar2(100), item Item_varray_COLL );
|
Note: The names of the object types and attributes in the preceding example can be system-generated.
If the |
To specify specific names of SQL objects generated include the attributes SQLName and SQLType in the XML schema definition prior to registering the XML schema.
SQLName and SQLType values, Oracle XML DB creates the SQL object types using these names.All annotations are in the form of attributes that can be specified within attribute and element declarations. These attributes belong to the Oracle XML DB namespace: http://xmlns.oracle.com/xdb
Table 5-2 lists Oracle XML DB attributes that you can specify in element and attribute declarations.
Information regarding the SQL mapping is stored in the XML schema document. The registration process generates the SQL types, as described in "Mapping of Types Using DBMS_XMLSCHEMA" and adds annotations to the XML schema document to store the mapping information. Annotations are in the form of new attributes.
The following XML schema definition shows how SQL mapping information is captured using SQLType and SQLName attributes:
declare doc varchar2(3000) := '<schema targetNamespace="http://www.oracle.com/PO.xsd" xmlns:po="http://www.oracle.com/PO.xsd" xmlns:xdb="http://xmlns.oracle.com/xdb" xmlns="http://www.w3.org/2001/XMLSchema"> <complexType name="PurchaseOrderType"> <sequence> <element name="PONum" type="decimal" xdb:SQLName="PONUM" xdb:SQLType="NUMBER"/> <element name="Company" xdb:SQLName="COMPANY" xdb:SQLType="VARCHAR2"> <simpleType> <restriction base="string"> <maxLength value="100"/> </restriction> </simpleType> </element> <element name="Item" xdb:SQLName="ITEM" xdb:SQLType="ITEM_T" maxOccurs="1000"> <complexType> <sequence> <element name="Part" xdb:SQLName="PART" xdb:SQLType="VARCHAR2"> <simpleType> <restriction base="string"> <maxLength value="1000"/> </restriction> </simpleType> </element> <element name="Price" type="float" xdb:SQLName="PRICE" xdb:SQLType="NUMBER"/> </sequence> </complexType> </element> </sequence> </complexType> <element name="PurchaseOrder" type="po:PurchaseOrderType"/> </schema>'; begin dbms_xmlschema.registerSchema('http://www.oracle.com/PO.xsd', doc); end;
Figure 5-2 shows how Oracle XML DB creates XML schema-based XMLType tables using an XML document and mapping specified in an XML schema. An XMLType table is first created and depending on how the storage is specified in the XML schema, the XML document is mapped and stored either as a CLOB in one XMLType column, or stored object-relationally and spread out across several columns in the table.

An XMLType table is first created and depending on how the storage is specified in the XML schema, the XML document is mapped and stored either as a CLOB in one XMLType column, or stored object-relationally and spread out across several columns in the table.
Use DBMS_XMLSCHEMA to set the mapping of type information for attributes and elements.
An attribute declaration can have its type specified in terms of one of the following:
simpleType, declared within this XML schema or in an external XML schemaref=".."), declared within this XML schema or in an external XML schemasimpleTypeIn all cases, the SQL type and associated information (length and precision) as well as the memory mapping information, are derived from the simpleType on which the attribute is based.
You can explicitly specify an SQLType value in the input XML schema document. In this case, your specified type is validated. This allows for the following specific forms of overrides:
An element declaration can specify its type in terms of one of the following:
complexType, specified within this XML schema document or in an external XML schema.ref="..."), which could itself be within this XML schema document or in an external XML schema.complexType.An element based on a complexType is, by default, mapped to an object type containing attributes corresponding to each of the sub-elements and attributes. However, you can override this mapping by explicitly specifying a value for SQLType attribute in the input XML schema. The following values for SQLType are permitted in this case:
These represent storage of the XML in a text or unexploded form in the database. The following special cases are handled:
complexTypes used to declare elements and elements declared within the complexType), the SQLInline attribute is forced to be "false" and the correct SQL mapping is set to REF XMLTYPE.maxOccurs > 1, a VARRAY type may need to be created.
SQLInline="false", a default table needs to be created. It is added to the table creation context. The name of the default table has either been specified by the user, or derived by mangling the element name.This section describes how XML schema definitions map XML schema simpleType to SQL object types. Figure 5-3 shows an example of this.
Table 5-5 through Table 5-8 list the default mapping of XML schema simpleType to SQL, as specified in the XML schema definition. For example:

If the XML schema specifies the datatype to be string with a maxLength value of less than 4000, it is mapped to a VARCHAR2 attribute of the specified length. However, if maxLength is not specified in the XML schema, it can only be mapped to a LOB. This is sub-optimal when most of the string values are small and only a small fraction of them are large enough to need a LOB.
Using XML schema, a complexType is mapped to an SQL object type as follows:
complexType are mapped to object attributes. The simpleType defining the XML attribute determines the SQL datatype of the corresponding attribute.complexType are also mapped to object attributes. The datatype of the object attribute is determined by the simpleType or complexType defining the XML element.If the XML element is declared with attribute maxOccurs > 1, it is mapped to a collection attribute in SQL. The collection could be a VARRAY (default) or nested table if the maintainOrder attribute is set to false. Further, the default storage of the VARRAY is in Ordered Collections in Tables (OCTs) instead of LOBs. You can choose LOB storage by setting the storeAsLob attribute to true.
By default, a sub-element is mapped to an embedded object attribute. However, there may be scenarios where out-of-line storage offers better performance. In such cases the SQLInline attribute can be set to false, and Oracle XML DB generates an object type with an embedded REF attribute. REF points to another instance of XMLType that corresponds to the XML fragment that gets stored out-of-line. Default XMLType tables are also created to store the out-of-line fragments.
Figure 5-4 illustrates the mapping of a complexType to SQL for out-of-line storage.

In this example element Addr's attribute, xdb:SQLInLine, is set to false.The resulting object type OBJ_T2 has a column of type XMLType with an embedded REF attribute. The REF attribute points to another XMLType instance created of object type OBJ_T1 in table Addr_tab. Addr_tab has columns Street and City. The latter XMLType instance is stored out-of-line.
declare doc varchar2(3000) := '<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.oracle.com/emp.xsd" xmlns:emp="http://www.oracle.com/emp.xsd" xmlns:xdb="http://xmlns.oracle.com/xdb"> <complexType name = "Employee" xdb:SQLType="OBJ_T2"> <sequence> <element name = "Name" type = "string"/> <element name = "Age" type = "decimal"/> <element name = "Addr" xdb:SQLInline = "false"> <complexType xdb:SQLType="OBJ_T1">