Top Banner
<ORACLE XML DB> <ORACLE XML DB> <Author fullName=‘Marouane <Author fullName=‘Marouane Bouzoubaa’/> Bouzoubaa’/> <Course title=‘XML & Data Management <Course title=‘XML & Data Management ‘/> ‘/> <Supervisor fullName=‘Dr. Hachim <Supervisor fullName=‘Dr. Hachim Haddouti’/> Haddouti’/> </ORACLE XML DB> </ORACLE XML DB>
62

Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1: Schemas Registration Inserting and Querying.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

<ORACLE XML DB><ORACLE XML DB><Author fullName=‘Marouane <Author fullName=‘Marouane

Bouzoubaa’/>Bouzoubaa’/>

<Course title=‘XML & Data Management <Course title=‘XML & Data Management ‘/> ‘/>

<Supervisor fullName=‘Dr. Hachim <Supervisor fullName=‘Dr. Hachim Haddouti’/>Haddouti’/>

</ORACLE XML </ORACLE XML DB>DB>

Page 2: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

OutlineOutline► Why XML in the DatabaseWhy XML in the Database► What is XML DB?What is XML DB?► XML DB ArchitectureXML DB Architecture► The XMLType DatatypeThe XMLType Datatype► Example1:Example1:

Schemas RegistrationSchemas Registration Inserting and Querying DataInserting and Querying Data Creating and Using IndicesCreating and Using Indices

► The XML DB RepositoryThe XML DB Repository► The XML DB APIsThe XML DB APIs► XML Future AdditionsXML Future Additions► SummarySummary

Page 3: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Common XML ArchitecturesCommon XML Architectures

Page 4: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Why XML in the Why XML in the Database?Database?

► Reduce maintenance costs of extra moving Reduce maintenance costs of extra moving parts; i.e. XML middleware.parts; i.e. XML middleware.

Eliminate separate ‘XML-processing’ layersEliminate separate ‘XML-processing’ layers► Keep applications standards-basedKeep applications standards-based

W3C, IETF, ANSI/ISO, J2EEW3C, IETF, ANSI/ISO, J2EE Eliminate proprietary file formats, message Eliminate proprietary file formats, message

formats, delimited columnsformats, delimited columns► Enable richer semantics and better Enable richer semantics and better

management for content-oriented management for content-oriented applicationsapplications

Store as XML vs. Files or LOBsStore as XML vs. Files or LOBs QueryabilityQueryability

Page 5: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Oracle and XMLOracle and XML

►Oracle has been at the forefront of XML Oracle has been at the forefront of XML technology for quite a while.technology for quite a while.

►From the beginning Oracle implemented From the beginning Oracle implemented the various different XML standards, the various different XML standards, which have largely been defined by the which have largely been defined by the W3C.W3C.

►Oracle XML parser: SAX and DOM APIs.Oracle XML parser: SAX and DOM APIs.►XSLT transformations and support for the XSLT transformations and support for the

initial XML Schema proposalsinitial XML Schema proposals►Etc… ahead of time.Etc… ahead of time.

Page 6: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

ORACLE XML DB FeaturesORACLE XML DB Features

► With Oracle XML DB, you get all the With Oracle XML DB, you get all the advantages of relational database technology advantages of relational database technology and XML technology at the same time.and XML technology at the same time.

► Oracle XML DB provides an independent Oracle XML DB provides an independent infrastructure to store and manage XML data:infrastructure to store and manage XML data: storage-independentstorage-independent content-independentcontent-independent programming language-independentprogramming language-independent

Page 7: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

ORACLE XML DBORACLE XML DB

► Oracle XML DB is the term used to describe Oracle XML DB is the term used to describe the technology in the Oracle 9i Release 2 the technology in the Oracle 9i Release 2 database for supporting XMLdatabase for supporting XML

► Extends the popular Oracle relational Extends the popular Oracle relational databasedatabase Keeps all the functionality of RDBMSKeeps all the functionality of RDBMS Provides all of the functionality associated with Provides all of the functionality associated with

a native XML databasea native XML database

► Delivers high-performance storage and Delivers high-performance storage and retrieval of XML.retrieval of XML.

Page 8: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML Support in Oracle 9i XML Support in Oracle 9i R2R2

XMLTypeXMLTypeXML RepositoryXML Repository

Page 9: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB Architecture - Data ViewXML DB Architecture - Data View

Page 10: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB Architecture - Content XML DB Architecture - Content ViewView

Page 11: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType Datatype The XMLType Datatype DefinitionDefinition

►““XMLType is a native data-XMLType is a native data-type that is used to store and type that is used to store and manage XML documents in manage XML documents in columns or tables”columns or tables”

Page 12: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType DatatypeThe XMLType Datatype

►Introduced with Oracle 9i Rel. Introduced with Oracle 9i Rel. 1 to store XML data in the 1 to store XML data in the databasedatabase

►Enhanced extensively in 9i Enhanced extensively in 9i Rel. 2Rel. 2

►XMLType is a native server data-XMLType is a native server data-typetype allows the database to understand allows the database to understand

that a column or table contains XMLthat a column or table contains XML same way as DATE data-typesame way as DATE data-type

Page 13: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType DatatypeThe XMLType Datatype►XMLType is an object typeXMLType is an object type► In tables: xmltype can either be In tables: xmltype can either be

used as data-type for a column or used as data-type for a column or an entire tablean entire table

create table orders create table orders (( orderNo NUMBER(10),orderNo NUMBER(10), customerId NUMBER(8),customerId NUMBER(8), xmlorder XMLTYPE,xmlorder XMLTYPE, date_entered DATEdate_entered DATE ))

---------------------------------------------------------------------------- create table orders of XMLTYPEcreate table orders of XMLTYPE

Page 14: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType DatatypeThe XMLType Datatype

►XMLType is an object typeXMLType is an object type In PL/SQL: a parameter, a return In PL/SQL: a parameter, a return

value, a PL/SQL table, a record or a value, a PL/SQL table, a record or a variable can be declared as an variable can be declared as an XMLTYPEXMLTYPE

Page 15: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType DatatypeThe XMLType Datatype

►The XMLType data type can also be The XMLType data type can also be used when defining views: used when defining views: XMLType view XMLType view relational view that includes an XMLType relational view that includes an XMLType

columncolumn

►This allows to expose content stored in This allows to expose content stored in relational tables and external data relational tables and external data sources as XML documents.sources as XML documents.

Page 16: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType DatatypeThe XMLType Datatype

► XMLType has a number of built-in XMLType has a number of built-in member functions for creating, member functions for creating, extracting and indexing XML dataextracting and indexing XML data

►Allows access to the XML data using Allows access to the XML data using standard XPath expressionsstandard XPath expressions

► A new 9.0.2 operator, XMLTransform, A new 9.0.2 operator, XMLTransform, allows for XSL transformationsallows for XSL transformations

► The XML data can optionally be The XML data can optionally be based on a W3C schemabased on a W3C schema

Page 17: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType Member The XMLType Member FunctionsFunctions

XML DB provides a number of functions for querying XML DB provides a number of functions for querying and updating XML data. The most important of and updating XML data. The most important of these functions are: these functions are:

► extractextract(xmltype,xpath) (xmltype,xpath) extracts a subset of extracts a subset of nodes, or an XML fragment, from an XML nodes, or an XML fragment, from an XML document document

► existsNodeexistsNode(xmltype,xpath) (xmltype,xpath) checks whether or not a checks whether or not a particular node exists in the XMLType, it particular node exists in the XMLType, it is often found is often found as part of a WHERE clause statement as part of a WHERE clause statement

► extractValueextractValue((xmltype,xpathxmltype,xpath) extracts the value ) extracts the value of a single node in an XML document using an of a single node in an XML document using an XPath expression XPath expression

► updateXMLupdateXML() updates an entire XML document () updates an entire XML document or a portion of the XML document in the database or a portion of the XML document in the database

Page 18: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType Member The XMLType Member FunctionsFunctions

►schemaValidateschemaValidate() () Validates the contents Validates the contents of the XMLType against an XML Schemaof the XMLType against an XML Schema

►transformtransform() () XSL TransformationXSL Transformation►getClobVal() getClobVal() Returns the document as Returns the document as

a CLOB. a CLOB. ►getNumVal() RgetNumVal() Returns the fragment or eturns the fragment or

text value in the XMLType to a number. text value in the XMLType to a number. ►getStringVal() getStringVal() Returns the document Returns the document

as a string. as a string.

Page 19: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType Data StorageXMLType Data Storage

► XMLType data can be stored in XMLType data can be stored in one of two ways:one of two ways: In CLOB storage to maintain In CLOB storage to maintain

the original XML contentthe original XML content In Structured storage (tables In Structured storage (tables

and views) to maintain DOM and views) to maintain DOM fidelityfidelity

Page 20: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType Data StorageXMLType Data Storage

►When you create an XMLType column When you create an XMLType column without an XML schema, a hidden without an XML schema, a hidden CLOB column is automatically createdCLOB column is automatically created

►The hidden column stores the extra The hidden column stores the extra non-SQL based informationnon-SQL based information

Page 21: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

CLOB VS. Structured StorageCLOB VS. Structured Storage

Page 22: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.
Page 23: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.
Page 24: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML Piecewise Update

►When using Structured Storage: You can use XPath to specify individual

elements and attributes of your document to update, without rewriting the entire document.

This is more efficient, especially for large XML documents.

Page 25: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType Data StorageXMLType Data Storage►Constraining a column or table to an XML Constraining a column or table to an XML

Schema has a number of advantages: Schema has a number of advantages: The database will store only valid XML The database will store only valid XML

documents documents Oracle XML DB can use the information Oracle XML DB can use the information

contained in the XML Schema to provide contained in the XML Schema to provide more intelligent query and update processing more intelligent query and update processing of the XML.of the XML.

►One important point to note: One important point to note: DTDs are DTDs are not supported.not supported.

Page 26: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType: Virtual DOMXMLType: Virtual DOM► DOM problem: to operate on a document, DOM problem: to operate on a document,

the whole document has to be an object the whole document has to be an object model in memory.model in memory.

► With large documents, that can be a serious With large documents, that can be a serious problem. problem.

► Virtual DOMVirtual DOM: however, allows users to : however, allows users to materialize a DOM on the fly while materialize a DOM on the fly while processing requests (a "virtual" or "lazy" processing requests (a "virtual" or "lazy" DOM), rather than doing it all at once; it DOM), rather than doing it all at once; it only loads the data trees as they are only loads the data trees as they are requested, throwing away previously-requested, throwing away previously-referenced sections of the document if referenced sections of the document if memory usage grows too large. This feature memory usage grows too large. This feature is completely transparent. is completely transparent.

Page 27: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType: Schema ValidationXMLType: Schema Validation► Oracle XML DB does not automatically Oracle XML DB does not automatically

perform a full schema validation of perform a full schema validation of documents as they are inserted into the documents as they are inserted into the database. database.

► Why? Why? ► Schema validation is a fairly CPU intensive Schema validation is a fairly CPU intensive

operation.operation.► The development team decided to make full The development team decided to make full

schema validation optional.schema validation optional.► Full schema validation can be enabled on a Full schema validation can be enabled on a

Schema by Schema basis using a Check Schema by Schema basis using a Check Constraint or Trigger.Constraint or Trigger.

Page 28: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XMLType: Schema ValidationXMLType: Schema Validation

►By default, Oracle XML DB performs a By default, Oracle XML DB performs a light weight validation of each light weight validation of each document. It checks that mandatory document. It checks that mandatory elements and attributes are present, elements and attributes are present, and that number of elements within a and that number of elements within a collection is compliant with the values collection is compliant with the values defined in the XMLSchema defined in the XMLSchema

► It does not check pattern matches, It does not check pattern matches, minimum lengths etc. This is a minimum lengths etc. This is a performance optimization. performance optimization.

Page 29: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

►XMLType Datatype XMLType Datatype ExampleExample

Page 30: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Example:Example: Scenario Scenario

A mediator Company that offers many A mediator Company that offers many products from different suppliersproducts from different suppliers

The client buys from different suppliersThe client buys from different suppliers

Page 31: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Example:Example: Steps To Steps To AccomplishAccomplish

► 1. Register the XML schema with XML DB 1. Register the XML schema with XML DB ► 2. Create a relational table to hold both 2. Create a relational table to hold both

relational data and XML documents relational data and XML documents ► 3. Load an XML document into the table 3. Load an XML document into the table ► 4. Write an SQL query to retrieve 4. Write an SQL query to retrieve

Whole DocumentWhole Document Data from both the relational fields and the Data from both the relational fields and the

XML document in the same queryXML document in the same query

5. Update an XML field5. Update an XML field6. Create Indexes6. Create Indexes

Page 32: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Example:Example: Schema File Schema Filehttp://stud.alakhawayn.ma/~991BE740226/schema/order.xsdhttp://stud.alakhawayn.ma/~991BE740226/schema/order.xsd

Page 33: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

► <?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>► <!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Marouane Bouzoubaa (AUI) --><!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Marouane Bouzoubaa (AUI) -->► <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">► <xs:element name="order"><xs:element name="order">► <xs:complexType><xs:complexType>► <xs:sequence><xs:sequence>► <xs:element ref="fillByDate" minOccurs="0"/><xs:element ref="fillByDate" minOccurs="0"/>► <xs:element name="items" type="itemsType"/><xs:element name="items" type="itemsType"/>► </xs:sequence></xs:sequence>► <xs:attribute name="orderNo" type="xs:positiveInteger"/><xs:attribute name="orderNo" type="xs:positiveInteger"/>► </xs:complexType></xs:complexType>► </xs:element></xs:element>► <xs:complexType name="itemsType"><xs:complexType name="itemsType">► <xs:sequence><xs:sequence>► <xs:element name="item" type="itemType" <xs:element name="item" type="itemType"

maxOccurs="unbounded"/>maxOccurs="unbounded"/>► </xs:sequence></xs:sequence>► </xs:complexType></xs:complexType>► <xs:complexType name="itemType"><xs:complexType name="itemType">► <xs:sequence><xs:sequence>► <xs:element ref="supplier"/><xs:element ref="supplier"/>► <xs:element ref="itemNo"/><xs:element ref="itemNo"/>► <xs:element ref="unitCost"/><xs:element ref="unitCost"/>► <xs:element ref="qty"/><xs:element ref="qty"/>► </xs:sequence></xs:sequence>► </xs:complexType></xs:complexType>► <xs:element name="qty" type="xs:positiveInteger"/><xs:element name="qty" type="xs:positiveInteger"/>► <xs:element name="supplier" type="xs:string"/><xs:element name="supplier" type="xs:string"/>► <xs:element name="fillByDate" type="xs:date"/><xs:element name="fillByDate" type="xs:date"/>► <xs:element name="itemNo" type="xs:string"/><xs:element name="itemNo" type="xs:string"/>► <xs:element name="unitCost"><xs:element name="unitCost">► <xs:simpleType><xs:simpleType>► <xs:restriction base="xs:decimal"><xs:restriction base="xs:decimal">► <xs:minInclusive value="0"/><xs:minInclusive value="0"/>► </xs:restriction></xs:restriction>► </xs:simpleType></xs:simpleType>► </xs:element></xs:element>► </xs:schema></xs:schema>

Page 34: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Example:Example: XML File XML Filehttp://stud.alakhawayn.ma/~991BE740226/schema/order1.xmlhttp://stud.alakhawayn.ma/~991BE740226/schema/order1.xml

Page 35: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

► <?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>► <!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Marouane Bouzoubaa (AUI) --><!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Marouane Bouzoubaa (AUI) -->► <order orderNo="104"><order orderNo="104">► <fillByDate>2003-08-28</fillByDate><fillByDate>2003-08-28</fillByDate>► <items><items>► <item><item>► <supplier>Miloud Company</supplier><supplier>Miloud Company</supplier>► <itemNo>SL35-721</itemNo><itemNo>SL35-721</itemNo>► <unitCost>17.97</unitCost><unitCost>17.97</unitCost>► <qty>12</qty><qty>12</qty>► </item></item>► <item><item>► <supplier>Spezi Company</supplier><supplier>Spezi Company</supplier>► <itemNo>CP01-487</itemNo><itemNo>CP01-487</itemNo>► <unitCost>162.75</unitCost><unitCost>162.75</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► <item><item>► <supplier>Kamal BO Company</supplier><supplier>Kamal BO Company</supplier>► <itemNo>ML32-42</itemNo><itemNo>ML32-42</itemNo>► <unitCost>12.25</unitCost><unitCost>12.25</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► <item><item>► <supplier>Al-Watania Company</supplier><supplier>Al-Watania Company</supplier>► <itemNo>KP96-351</itemNo><itemNo>KP96-351</itemNo>► <unitCost>16.70</unitCost><unitCost>16.70</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► <item><item>► <supplier>Bouchta Company</supplier><supplier>Bouchta Company</supplier>► <itemNo>CT01-365</itemNo><itemNo>CT01-365</itemNo>► <unitCost>62.05</unitCost><unitCost>62.05</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► </items></items>► </ order></ order>

Page 36: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Registering a SchemaRegistering a Schema

►Before using an XML schema, it must Before using an XML schema, it must be registered with the databasebe registered with the database

►This is accomplished using the This is accomplished using the DBMS_XMLSCHEMA packageDBMS_XMLSCHEMA package

►The two main functions are The two main functions are registerSchema and deleteSchemaregisterSchema and deleteSchema

Page 37: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Registering an XML Registering an XML SchemaSchema

exec DBMS_XMLSCHEMA.registerSchema(exec DBMS_XMLSCHEMA.registerSchema( schema URL, schema source document)schema URL, schema source document)

►The XML schema source document The XML schema source document can be define as a VARCHAR, a can be define as a VARCHAR, a CLOB, an XMLType or a URITypeCLOB, an XMLType or a URIType

►Registering an XML schema adds a Registering an XML schema adds a resource to the XML DB repositoryresource to the XML DB repository

►A Schema can be registered locally A Schema can be registered locally or globallyor globally

Page 38: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Registering an XML SchemaRegistering an XML Schema

►By default, schemas are loaded locally By default, schemas are loaded locally and associated with the “database and associated with the “database schema” they are created underschema” they are created under

► BEGIN BEGIN ► DBMS_XMLSCHEMA.REGISTERSCHEMA(DBMS_XMLSCHEMA.REGISTERSCHEMA(► schemaurl=>'http://stud.alakhawayn.ma/~991BE740226/schema/schemaurl=>'http://stud.alakhawayn.ma/~991BE740226/schema/

order.xsd', order.xsd', schemadoc=>sys.UriFactory.getUri('http://stud.alakhawayn.ma/~991BEschemadoc=>sys.UriFactory.getUri('http://stud.alakhawayn.ma/~991BE740226/schema/order.xsd')); 740226/schema/order.xsd'));

► END; END; ► //

Page 39: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Creating a Relational Table Creating a Relational Table with XML Datawith XML Data

►create table orders( create table orders( ►orderNo NUMBER(10),orderNo NUMBER(10),►customerId NUMBER(8),customerId NUMBER(8),►xmlOrder XMLTYPE,xmlOrder XMLTYPE,

►date_entered DATEdate_entered DATE))

► XMLTYPE COLUMN xmlOrder XMLTYPE COLUMN xmlOrder STORE ASSTORE AS CLOBCLOB STORAGE (INITIAL 4096 NEXT 4096) STORAGE (INITIAL 4096 NEXT 4096) XMLSCHEMA XMLSCHEMA

“http://stud.alakhawayn.ma/~991BE740226/schema/order.xsd” “http://stud.alakhawayn.ma/~991BE740226/schema/order.xsd” ELEMENT “order”ELEMENT “order”

;;

Page 40: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Inserting, Querying and Inserting, Querying and UpdatingUpdating

► XML data is inserted into an XMLType XML data is inserted into an XMLType column using the XMLType() constructorcolumn using the XMLType() constructor

INSERT INTO ORDERS (orderNo,customerId,INSERT INTO ORDERS (orderNo,customerId, xmlOrder, date_entered)xmlOrder, date_entered) VALUES (orderNoSeq.nextval, 6135,VALUES (orderNoSeq.nextval, 6135, XMLTYPE(‘XMLTYPE(‘

► <order orderNo="104" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://stud.alakhawayn.ma/~991BE740226/schema/order.xsd"><order orderNo="104" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://stud.alakhawayn.ma/~991BE740226/schema/order.xsd">► <fillByDate>2003-08-28</fillByDate><fillByDate>2003-08-28</fillByDate>► <items><items>► <item><item>► <supplier>Miloud Company</supplier><supplier>Miloud Company</supplier>► <itemNo>SL35-721</itemNo><itemNo>SL35-721</itemNo>► <unitCost>17.97</unitCost><unitCost>17.97</unitCost>► <qty>12</qty><qty>12</qty>► </item></item>► <item><item>► <supplier>Spezi Company</supplier><supplier>Spezi Company</supplier>► <itemNo>CP01-487</itemNo><itemNo>CP01-487</itemNo>► <unitCost>162.75</unitCost><unitCost>162.75</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► <item><item>► <supplier>SOMAPHA Company</supplier><supplier>SOMAPHA Company</supplier>► <itemNo>ML32-42</itemNo><itemNo>ML32-42</itemNo>► <unitCost>12.25</unitCost><unitCost>12.25</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>

► <item><item>► <supplier>Al-Watania Company</supplier><supplier>Al-Watania Company</supplier>► <itemNo>KP96-351</itemNo><itemNo>KP96-351</itemNo>► <unitCost>16.70</unitCost><unitCost>16.70</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>

► <item><item>► <supplier>Bouchta Company</supplier><supplier>Bouchta Company</supplier>► <itemNo>CT01-365</itemNo><itemNo>CT01-365</itemNo>► <unitCost>62.05</unitCost><unitCost>62.05</unitCost>► <qty>5</qty><qty>5</qty>► </item></item>► </items></items>► </order></order>

'), SYSDATE)'), SYSDATE)

Page 41: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Inserting, Querying and Inserting, Querying and UpdatingUpdating

► The XML document can be retrieved in a query The XML document can be retrieved in a query using getClobVal()using getClobVal()

► select o.xmlOrder.getClobVal() from select o.xmlOrder.getClobVal() from ORDERS oORDERS o

WHERE WHERE o.xmlOrder.existsNode('/order/items')= 1o.xmlOrder.existsNode('/order/items')= 1

► select extract(o.xmlOrder,’/’) from select extract(o.xmlOrder,’/’) from ORDERS oORDERS o

WHERE WHERE o.xmlOrder.existsNode('/order/items')= 1o.xmlOrder.existsNode('/order/items')= 1

Page 42: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

HintHint

► In order to display many lines of the In order to display many lines of the XML result in the sql*plus:XML result in the sql*plus:

►set long 10000set long 10000►Set pagesize 10000Set pagesize 10000

Page 43: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Inserting, Querying and Inserting, Querying and UpdatingUpdating

►You can even combine both types of You can even combine both types of access into a single SQL queryaccess into a single SQL query

SELECT orderNo, customerId, SELECT orderNo, customerId, extractValue(xmlOrder, extractValue(xmlOrder, '/order/fillByDate') FillByDate, '/order/fillByDate') FillByDate, date_entered FROM ordersdate_entered FROM orders

Page 44: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Inserting, Querying and Inserting, Querying and UpdatingUpdating

►The XML document can be updated The XML document can be updated usingusing

►update orders SET xmlOrder = update orders SET xmlOrder = updateXML(xmlOrder,updateXML(xmlOrder,

'/order/items/item[2]/qty/text()',10'/order/items/item[2]/qty/text()',10););

Page 45: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Creating XMLType IndicesCreating XMLType Indices►Indices can be created on Indices can be created on

XMLType columnsXMLType columns

►These indices can either be These indices can either be function-based using extract() or function-based using extract() or existNodes(). Latter is better for existNodes(). Latter is better for bit-mapped indexesbit-mapped indexes

Page 46: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Creating XMLType IndicesCreating XMLType Indices

► CREATE INDEX FillBy_NDX ON ORDERSCREATE INDEX FillBy_NDX ON ORDERS

(xmlOrder.extract('/order/fillByDate').(xmlOrder.extract('/order/fillByDate').

getStringVal());getStringVal());

► CREATE BITMAP INDEX MultItems_NDX ON CREATE BITMAP INDEX MultItems_NDX ON ORDERSORDERS

(xmlOrder.existsNode('/order/items/(xmlOrder.existsNode('/order/items/

item/supplier'));item/supplier'));

Page 47: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Creating Context IndexesCreating Context Indexes►Oracle ConText indexes can be Oracle ConText indexes can be

defined for XMLTypes columns or defined for XMLTypes columns or tablestables

CREATE INDEX co_ctx_ndx ON CREATE INDEX co_ctx_ndx ON orders(xmlOrder) indextype is orders(xmlOrder) indextype is ctxsys.context;ctxsys.context;

►Operation such as CONTAINS can Operation such as CONTAINS can then be usedthen be used

Page 48: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

The XMLType/XML DB APIsThe XMLType/XML DB APIs► XMLType Member FunctionsXMLType Member Functions► SYS_XMLGEN, SYS_XMLAGG and SYS_XMLGEN, SYS_XMLAGG and

DBMX_XMLGENDBMX_XMLGEN► SQL/XML FunctionsSQL/XML Functions► PL/SQL DOM API (DBMS_XMLDOM)PL/SQL DOM API (DBMS_XMLDOM)► PL/SQL Parser API (DBMS_XML_PARSER)PL/SQL Parser API (DBMS_XML_PARSER)► XSLT Processor (DBMS_XSL_ XSLT Processor (DBMS_XSL_

PROCESSOR)PROCESSOR)► DBMS_XDBT (Context indexes)DBMS_XDBT (Context indexes)► The XML DB Repository viewsThe XML DB Repository views

Page 49: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

►XML DB RepositoryXML DB Repository

Page 50: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB RepositoryXML DB Repository

► It is possible to have your cake and eat It is possible to have your cake and eat it tooit too

►You can store your document as a You can store your document as a Native XML Type in Oracle's XML DB Native XML Type in Oracle's XML DB repository, which will preserve byte-repository, which will preserve byte-by-byte document fidelity and also by-byte document fidelity and also shred it into SQL tables.shred it into SQL tables.

Page 51: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB RepositoryXML DB Repository

►When your XML schema is registered, When your XML schema is registered, you store your XML data in your you store your XML data in your database, by simply inserting an XML database, by simply inserting an XML document file using SQL, PL/SQL, Java, document file using SQL, PL/SQL, Java, FTP, HTTP, or WebDAV.FTP, HTTP, or WebDAV.

►Getting XML data out of your database Getting XML data out of your database can be as simple as executing a SQL can be as simple as executing a SQL query or reading a file using one of query or reading a file using one of those Internet-standard protocols.those Internet-standard protocols.

Page 52: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB Repository XML DB Repository FeaturesFeatures

► Provides access to XML content using a Provides access to XML content using a foldering metaphorfoldering metaphor

► Includes ACLs (Access Control Lists) to Includes ACLs (Access Control Lists) to control secured access to XML datacontrol secured access to XML data

► Access includes HTTP, WedDAV and FTPAccess includes HTTP, WedDAV and FTP► WebDAV provides directory-like access to WebDAV provides directory-like access to

XML from any WebDAV-enabled device XML from any WebDAV-enabled device such as Windows desktopssuch as Windows desktops

► FTP access is provided for XML FTP access is provided for XML application development requiring file application development requiring file transferstransfers

► Document versioning is also availableDocument versioning is also available

Page 53: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.
Page 54: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB RepositoryXML DB Repository

►There are many advantages to There are many advantages to the XML DB repository, but it the XML DB repository, but it isn't right for every application.isn't right for every application.

►Overhead is involved in Overhead is involved in maintaining the relationship maintaining the relationship between the full document and between the full document and its shredded data. its shredded data.

Page 55: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB RepositoryXML DB Repository

►However, the biggest problem comes However, the biggest problem comes with schema evolution. with schema evolution.

►That means you can't do most That means you can't do most nontrivial changes of either the nontrivial changes of either the database or the document schema, database or the document schema, without having to export all the data without having to export all the data and re-import it into the database.and re-import it into the database.

Page 56: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML DB: Comming SoonXML DB: Comming Soon

► In the future, Oracle is looking at In the future, Oracle is looking at adding several new capabilities:adding several new capabilities:

►XQuery: which will be a language XQuery: which will be a language specifically designed to query XML data specifically designed to query XML data from a document perspective, rather from a document perspective, rather than the rows-and-tables perspective of than the rows-and-tables perspective of SQL.SQL.

►(A prototype of XQuery language is (A prototype of XQuery language is available for available for downloaddownload on OTN.) on OTN.)

Page 57: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

SummarySummary

Page 58: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

Oracle XML DBOracle XML DB

►Oracle XML DBOracle XML DB Adds native support for the emerging XML Adds native support for the emerging XML

standards to the popular Oracle databasestandards to the popular Oracle database Brings SQL and XML processing together Brings SQL and XML processing together introduces a number of innovations introduces a number of innovations

needed for efficient storage and retrieval needed for efficient storage and retrieval of XML.of XML.

Page 59: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

XML-Type SummayXML-Type Summay

► XML can be stored one of two ways: XML can be stored one of two ways: An XMLType column in a relational table.An XMLType column in a relational table. An XML object in an XMLType table.An XML object in an XMLType table.

► Non Schema based XML is always stored as Non Schema based XML is always stored as CLOB.CLOB.

► Schema based XML can be stored as a CLOB Schema based XML can be stored as a CLOB or as a set of objects.or as a set of objects.

► Relational and external data can be exposed Relational and external data can be exposed as XML using views.as XML using views.

► The view can be a relational view containing a The view can be a relational view containing a column of XMLType or can be a XMLType column of XMLType or can be a XMLType View.View.

Page 60: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

CLOB vs. Structured StorageCLOB vs. Structured Storage

UnstructuredUnstructured

Whole Whole documentdocument

Original contentOriginal content

Mediocre for Mediocre for DML DML

Can be largeCan be large

StructuredStructured

DecomposedDecomposed

DOM FidelityDOM Fidelity

Excellent for Excellent for DMLDML

Less spaceLess space

Format

Storage

Integrity

Performance

Space

Feature CLOB Structured

Page 61: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.

References References ► 1. Oracle 9i XML Database Developer's Guide: Appendix A – 1. Oracle 9i XML Database Developer's Guide: Appendix A –

Installing and Configuring XML DB. Installing and Configuring XML DB. ((http://otn.oracle.com/tech/xml/doc.htmlhttp://otn.oracle.com/tech/xml/doc.html))

► 2. Oracle 9i: XML API Reference. 2. Oracle 9i: XML API Reference. ((http://otn.oracle.com/tech/xml/doc.htmlhttp://otn.oracle.com/tech/xml/doc.html))

► 3. W3C XML 1.0 Recommendation. (http://www.w3.org/TR/REC-3. W3C XML 1.0 Recommendation. (http://www.w3.org/TR/REC-xml)xml)

► 4. W3C XML Schema Recommendation. 4. W3C XML Schema Recommendation. ((http://www.w3.org/TR/xmlschema-1 http://www.w3.org/TR/xmlschema-1 and and http://www.w3.org/TR/xmlschema-2 http://www.w3.org/TR/xmlschema-2 ) )

► 5. Altova's XML Spy. (5. Altova's XML Spy. (http://www.xmlspy.comhttp://www.xmlspy.com))► 6. Oracle 9i XML Database Developer's Guide: Appendix G – 6. Oracle 9i XML Database Developer's Guide: Appendix G –

Example Setup Scripts. At the beginning of this section, there is Example Setup Scripts. At the beginning of this section, there is a script that you can run to create a directory to contain your a script that you can run to create a directory to contain your schemas and XML documents as well as for getDocument() schemas and XML documents as well as for getDocument() procedure to load these files into XML DB procedure to load these files into XML DB ((http://otn.oracle.com/tech/xml/doc.htmlhttp://otn.oracle.com/tech/xml/doc.html))

Page 62: Outline ► Why XML in the Database ► What is XML DB? ► XML DB Architecture ► The XMLType Datatype ► Example1:  Schemas Registration  Inserting and Querying.