36
XML dokumentu shēmas lietošana Oracle XML DB 1. Tiek izmantotas W3C saderīgas XML shēmas: - lokālas; - globalas. 2. XMLType tabulu, skatu un kolonu veidošana. 3. XML dokumentu pareizības pārbaude. 4. XML skēmu ģenerēšana no SQL objektu tipiem. 5. Manipulating and querying XML schema-based XMLType tables. 6. Generating a database mapping from XML schemas during XML schema registration. This includes generating SQL object types, collection types, and default tables, and capturing the mapping information using XML schema attributes. 7. Automatically inserting data into default tables when XML schema-based documents are inserted into Oracle XML DB Repository using protocols (FTP, HTTP(S)/WebDAV) and languages besides SQL. 1

XML dokumentu shēmas lietošana Oracle XML DB€¦  · Web viewXML dokumentu shēmas lietošana Oracle XML DB . 1. Tiek izmantotas . W3C saderīgas XML shē. mas: - lokā. l. as;

Embed Size (px)

Citation preview

XML dokumentu shēmas lietošana Oracle XML DB

1. Tiek izmantotas W3C saderīgas XML shēmas:

- lokālas;

- globalas.

2. XMLType tabulu, skatu un kolonu veidošana.

3. XML dokumentu pareizības pārbaude.

4. XML skēmu ģenerēšana no SQL objektu tipiem.

5. Manipulating and querying XML schema-based XMLType tables.

6. Generating a database mapping from XML schemas during XML schema

registration. This includes generating SQL object types, collection types, and

default tables, and capturing the mapping information using XML schema

attributes.

7. Automatically inserting data into default tables when XML schema-based

documents are inserted into Oracle XML DB Repository using protocols (FTP,

HTTP(S)/WebDAV) and languages besides SQL.

1

XML dokumentu shēmas reģistrēšana XML datu bāzē1

Pirms XML dokumentu shēma tiek izmantota XML datu bāzē, tā ir jāreģistrē.

Shēmas reģistrāciju veic izmantojot PL/SQL programmu paketi

DBMS_XMLSCHEMA.

Tiek izmantotas sekojošas procedūras:

1) registerSchema – shēmas reģistrācija;

2) deleteSchema – iepriekš reģistrētās shēmas nodzēšana;

3) copyEvolve – iepriekš reģistrētās shēmas modificēšana.

1 http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28369/xdb_dbmstore.htm

2

XML dokumenta shēmas reģistrēšanas komanda

DBMS_XMLSCHEMA.REGISTERSCHEMA(schemaurl IN VARCHAR2, schemadoc IN SYS.URIType, local IN BOOLEAN := TRUE, gentypes IN BOOLEAN := TRUE, genbean IN BOOLEAN := FALSE,force IN BOOLEAN := FALSE,owner IN VARCHAR2 := NULL,enablehierarchy IN PLS_INTEGER :=

DBMS_XMLSCHEMA.ENABLE_CONTENTS,options IN PLS_INTEGER := 0);schemaurl URL that uniquely identifies the schema document. This value is used to derive the path name of the schema document within the database hierarchy. Can be used inside schemalocation attribute of XML Schema import element.schemadoc A valid XML schema documentlocal Is this a local or global schema?By default, all schemas are registered as local schemas, under /sys/schemas/<username>/...If a schema is registered as global, it is added under /sys/schemas/PUBLIC/...You need write privileges on the directory to be able to register a schema as global.gentypes Determines whether the schema compiler generates object types. By default, TRUE.genbean Determines whether the schema compiler generates Java beans. By default, FALSE.gentables Determines whether the schema compiler generates default tables. By default, TRUEforce If this parameter is set to TRUE, the schema registration will not raise errors. Instead, it creates an invalid XML schema object in case of any errors. By default, the value of this parameter is FALSE.owner This parameter specifies the name of the database user owning the XML schema object. By default, the user registering the schema owns the XML schema object. This parameter can be used to register a XML schema to be owned by a different database user.csid Identifies the character set of the input schema document. If this value is 0, the schema document's encoding is determined by the current rule for "text/xml" MIME type.enablehierarchyENABLE_HIERARCHY_NONE - enable hierarchy will not be called on any tables created while registering that schemaENABLE_HIERARCHY_CONTENTS - enable hierarchy will be called for all tables created during schema registration with hierarchy_type as DBMS_XDBZ.ENABLE_CONTENTS. This is the default.ENABLE_HIERARCHY_RESMETADATA - enable hierarchy will be called on all tables created during schema registration with hierarchy_type as DBMS_XDBZ.ENABLE_RESMETADATA. Users should pass in DBMS_XMLSCHEMA.ENABLE_RESMETADATA for schemas they intend to use as resource metadata tables.options Additional options to specify how the schema should be registered. The various options are represented as bits of an integer and the options parameter should be constructed by doing a BITOR of the desired bits. Possible bits:REGISTER_NODOCID - this will suppress the creation of the DOCID column for out of line tables. This is a storage optimization which might be desirable when we do not need to join back to the document table (for example if we do not care about rewriting certain queries that could be rewritten by making use of the DOCID column)

3

XML dokumenta shēmas reģistrēšana

create or replace directory XMLDIR as 'G:/____GRAMATA/11_XML_DB/XML_piemeri';

beginDBMS_XMLSCHEMA.REGISTERSCHEMA(SCHEMAURL => 'http://localhost:8080/public/gramatveikals.xsd',SCHEMADOC => bfilename('XMLDIR', 'gramatveikals.xsd'),LOCAL => TRUE,CSID => nls_charset_id('AL32UTF8') ) ;end;

SCHEMAURL – shēmas URL (adrese), unikāla XML datu bāzē. Parasti URL formātā, bet tas nav obligāti.

SCHEMADOC – shēmas avota dokuments. Var būt VARCHAR, CLOB, BLOB, BFILE, XMLTypevai URIType tipi.

CSID – simbolu kopas kodējums, ja tiek lietoti BFILE vai BLOB tipi.

OPTIONS – norādā kā shēma tiek reģistrēta:REGISTER_BINARYXML – binārais glabāšanas variants (GENTYPES => FALSE); REGISTER_AUTO_OOL;REGISTER_NT_AS_IOT.

Ja GENTYPES => TRUE, shēma tiek reģistrēta lai izmantotu strukturētam XML datu glabāšanas variantam.

4

XML dokumentu shēmas metadatu iegūšana no datu bāzes vārdnīcas

Var tikt izmantoti sekojoši DB vārdnīcas skati:1) USER_XML_SCHEMAS;2) ALL_XML_SCHEMAS;3) USER_XML_TABLES;4) ALL_XML_TABLES.

describe USER_XML_SCHEMAS;

select LOCAL, SCHEMA_URL, QUAL_SCHEMA_URLfrom USER_XML_SCHEMAS;

5

select SCHEMA from USER_XML_SCHEMAS;

<?xml version="1.0" encoding="WINDOWS-1257"?><xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:oraxdb="http://xmlns.oracle.com/xdb" oraxdb:storeVarrayAsTable="true" oraxdb:flags="8487" oraxdb:schemaURL="http://localhost:8080/public/gramatveikals.xsd" oraxdb:schemaOwner="SYSTEM" oraxdb:numProps="13"> <xsd:element name="gramatveikals" oraxdb:propNumber="4171" oraxdb:global="true" oraxdb:SQLName="gramatveikals" oraxdb:SQLType="gramatveikals671_T" oraxdb:SQLSchema="SYSTEM" oraxdb:memType="258" oraxdb:defaultTable="gramatveikals673_TAB" oraxdb:defaultTableSchema="SYSTEM"> <xsd:complexType oraxdb:SQLType="gramatveikals671_T" oraxdb:SQLSchema="SYSTEM"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="gramata" oraxdb:propNumber="4170" oraxdb:global="false" oraxdb:SQLName="gramata" oraxdb:SQLType="gramata667_T" oraxdb:SQLSchema="SYSTEM" oraxdb:memType="258" oraxdb:MemInline="false" oraxdb:SQLInline="true" oraxdb:JavaInline="false" oraxdb:SQLCollType="gramata672_COLL" oraxdb:SQLCollSchema="SYSTEM"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="gramata" oraxdb:propNumber="4177" oraxdb:global="true" oraxdb:SQLName="gramata" oraxdb:SQLType="gramata667_T" oraxdb:SQLSchema="SYSTEM" oraxdb:memType="258" oraxdb:defaultTable="gramata670_TAB" oraxdb:defaultTableSchema="SYSTEM"> <xsd:complexType mixed="true" oraxdb:SQLType="gramata667_T" oraxdb:SQLSchema="SYSTEM"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="virsraksts" oraxdb:propNumber="4173" oraxdb:global="false" oraxdb:SQLName="virsraksts" oraxdb:SQLType="virsraksts665_T" oraxdb:SQLSchema="SYSTEM" oraxdb:memType="258" oraxdb:MemInline="false" oraxdb:SQLInline="true" oraxdb:JavaInline="false" oraxdb:SQLCollType="virsraksts668_COLL" oraxdb:SQLCollSchema="SYSTEM"/> <xsd:element ref="autors" oraxdb:propNumber="4174" oraxdb:global="false" oraxdb:SQLName="autors" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:MemInline="false" oraxdb:SQLInline="true" oraxdb:JavaInline="false" oraxdb:SQLCollType="autors669_COLL" oraxdb:SQLCollSchema="SYSTEM"/> <xsd:element ref="gads" oraxdb:propNumber="4175" oraxdb:global="false" oraxdb:SQLName="gads" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:MemInline="false" oraxdb:SQLInline="true" oraxdb:JavaInline="false" oraxdb:SQLCollType="autors669_COLL" oraxdb:SQLCollSchema="SYSTEM"/> <xsd:element ref="cena" oraxdb:propNumber="4176" oraxdb:global="false" oraxdb:SQLName="cena" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:MemInline="false" oraxdb:SQLInline="true" oraxdb:JavaInline="false" oraxdb:SQLCollType="autors669_COLL" oraxdb:SQLCollSchema="SYSTEM"/> </xsd:choice> <xsd:attribute name="kategorija" type="xsd:string" use="required" oraxdb:propNumber="4172" oraxdb:global="false" oraxdb:SQLName="kategorija" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1"/> </xsd:complexType> </xsd:element> <xsd:element name="virsraksts" oraxdb:propNumber="4179" oraxdb:global="true" oraxdb:SQLName="virsraksts" oraxdb:SQLType="virsraksts665_T" oraxdb:SQLSchema="SYSTEM" oraxdb:memType="258" oraxdb:defaultTable="virsraksts666_TAB" oraxdb:defaultTableSchema="SYSTEM"> <xsd:complexType oraxdb:SQLType="virsraksts665_T" oraxdb:SQLSchema="SYSTEM"> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="valoda" type="xsd:string" use="required" oraxdb:propNumber="4178" oraxdb:global="false" oraxdb:SQLName="valoda" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1"/> </xsd:extension> </xsd:simpleContent>

6

</xsd:complexType></xsd:element>

<xsd:element name="autors" type="xsd:string" oraxdb:propNumber="4180" oraxdb:global="true" oraxdb:SQLName="autors" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:defaultTable="autors664_TAB" oraxdb:defaultTableSchema="SYSTEM"/>

<xsd:element name="gads" type="xsd:string" oraxdb:propNumber="4181" oraxdb:global="true" oraxdb:SQLName="gads" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:defaultTable="gads663_TAB" oraxdb:defaultTableSchema="SYSTEM"/>

<xsd:element name="cena" type="xsd:string" oraxdb:propNumber="4182" oraxdb:global="true" oraxdb:SQLName="cena" oraxdb:SQLType="VARCHAR2" oraxdb:memType="1" oraxdb:defaultTable="cena662_TAB" oraxdb:defaultTableSchema="SYSTEM"/></xsd:schema>

7

select LOCAL, SCHEMA_URL, QUAL_SCHEMA_URL from USER_XML_SCHEMAS;

LOC SCHEMA_URL QUAL_SCHEMA_URL-------------------------------------------------------------------------------------------YEShttp://localhost:8080/public/gramatveikals.xsdhttp://xmlns.oracle.com/xdb/schemas/SYSTEM/localhost:8080/public/gramatveikals.xsd

SELECT ELEMENT_NAME, TABLE_NAME, STORAGE_TYPE FROM USER_XML_TABLESWHERE XMLSCHEMA = 'http://localhost:8080/public/gramatveikals.xsd';

ELEMENT_NAME TABLE_NAME STORAGE_TYPE------------------------------ ---------------------------------------------------gramatveikals GRAMATVEIKALS_SH OBJECT-RELATIONALgramatveikals gramatveikals272_TAB OBJECT-RELATIONALgramata gramata269_TAB OBJECT-RELATIONALvirsraksts virsraksts265_TAB OBJECT-RELATIONALautors autors263_TAB CLOBgads gads262_TAB CLOBcena cena261_TAB CLOB

describe "gramata269_TAB";Name Null? Type ------------------------------------------------------------------------------------------------TABLE of SYS.XMLTYPE(XMLSchema "http://localhost:8080/public/gramatveikals.xsd" Element "gramata") STORAGE Object-relational TYPE "gramata266_T"

describe "GRAMATVEIKALS_SH";Name Null? Type ----------------------------------------- -------- ----------------------------TABLE of SYS.XMLTYPE(XMLSchema "http://localhost:8080/public/gramatveikals.xsd" Element "gramatveikals") STORAGE Object-relational TYPE "gramatveikals270_T"

8

Kopējā DB shēmu shēma (root xml schema)

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.

9

XML dokumenta shēmas nodzēšana

begin DBMS_XMLSCHEMA.DELETESCHEMA( SCHEMAURL => 'http://localhost:8080/public/gramatveikals.xsd', DELETE_OPTION => DBMS_XMLSCHEMA.DELETE_CASCADE_FORCE);end;

DELETE_RESTRICT (pēc noklusēšanas) – nenodzēš bināro glabāšanas variantu.DELETE_CASCADE - nenodzēš bināro glabāšanas variantu.DBMS_XMLSCHEMA.purgeSchema – pilnībā nodzēš shēmu (11g versija).

select SCHEMA_URL from USER_XML_SCHEMAS;no rows selected

This does the following, by default:1. Checks that the current user has the appropriate privileges to delete the resource corresponding to the XML schema within Oracle XML DB Repository. You can control which users can delete which XML schemas, by setting the appropriate ACLs on the XML schema resources.2. Checks whether there are any tables dependent on the XML schema to be deleted. If so, raises an error and cancels the deletion. This check is not performed if option delete_invalidate or delete_cascade_force is used; in that case, no error is raised.3. Removes the XML schema document from the Oracle XML DB Repository (folder /sys/schemas).4. Removes the XML schema document from DBA_XML_SCHEMAS, unless it was registered for use with binary XML instances and neither delete_invalidate nor delete_cascade_force is used.5. Drops the default table, if either delete_cascade or delete_cascade_force is used. Raises an error if delete_cascade_force is specified and there are instances in other tables that are also dependent on the XML schema.

10

Shēmas reģistrēšana binārajam glabāšanas variantam

begin

DBMS_XMLSCHEMA.registerSchema(

SCHEMAURL => 'http://localhost:8080/public/majas.xsd',

SCHEMADOC => bfilename('XMLDIR', 'majas.xsd'),

CSID => nls_charset_id('AL32UTF8'),

GENTYPES => false,

OPTIONS => REGISTER_BINARYXML); -- Oracle11g versija

end;

11

XMLType tipa tabulas definēšana, kurai norādīta XML shēma

create table GRAMATVEIKALS_SH of XMLTypeXMLSCHEMA "http://xmlns.oracle.com/xdb/schemas/SYSTEM/localhost:8080/public/gramatveikals.xsd"ELEMENT "gramatveikals";

describe GRAMATVEIKALS_SH; Name Null? Type ------------------------------------------------------------------------------------------------------TABLE of SYS.XMLTYPE(XMLSchema"http://localhost:8080/public/gramatveikals.xsd" Element "gramatveikals") STORAGE Object-relational TYPE "gramatveikals270_T"

JDeveloper izmantošana struktūru apskatei

XML dokumentu datu ievade teksta veidā

insert into GRAMATVEIKALS_SH values (

12

XMLType ('<?xml version="1.0" encoding="windows-1257"?><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>'));1 row created.

insert into GRAMATVEIKALS_SH values (XMLType ('<?xml version="1.0" encoding="windows-1257"?><gramatveikals> <gramata kategorija="Tehniskā literatūra"> <virsraksts valoda="latviešu">XML pamati</virsraksts> <autors>Juris Kālis</autors> <gads>2009</gads> <cena>25.00</cena> </gramata></gramatveikals>'));1 row created.

13

XML dokumenta datu ievade no ārējā faila

1. XML failu kataloga definēšana:

create or replace directory XMLDIR as 'F:/XML_DOCS';

2. XML datu ievade no ārējā faila:

insert into GRAMATVEIKALS_SH values(XMLTYPE(bfilename('XMLDIR', 'gramatveikals.xml'), nls_charset_id('AL32UTF8')));1 row created.

select Value(A) from GRAMATVEIKALS_SH A;VALUE(A)--------------------------------------------------------------------------------<?xml version="1.0" encoding="WINDOWS-1257"?><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>

<?xml version="1.0" encoding="WINDOWS-1257"?><gramatveikals> <gramata kategorija="Tehniskā literatūra"> <virsraksts valoda="latviešu">XML pamati</virsraksts> <autors>Juris Kālis</autors> <gads>2009</gads> <cena>25.00</cena> </gramata></gramatveikals>

<?xml version="1.0" encoding="WINDOWS-1257"?><!-- New document created with EditiX at Fri Dec 02 17:23:04 EET 2011 --><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>

14

XML dokumentu datu ievade tabulā ar XMLType tipa kolonu

create table GRAMATVEIKALS_SH_KOL (ID NUMBER,DOK_XML XMLType)XMLTYPE COLUMN DOK_XMLELEMENT "http://localhost:8080/public/gramatveikals.xsd#gramatveikals";

There are two ways to specify XMLSchema and Element:1) as separate clauses, XMLSchema and Element;2) using only the Element clause with an XPointer notation.

insert into GRAMATVEIKALS_SH_KOL values (1, XMLType ('<?xml version="1.0" encoding="windows-1257"?><gramatveikals> <gramata kategorija="Tehniskā literatūra"> <virsraksts valoda="latviešu">XML pamati</virsraksts> <autors>Juris Kālis</autors> <gads>2009</gads> <cena>25.00</cena> </gramata></gramatveikals>'));1 row created.

15

Lielo objektu (CLOB) XML datu glabāšanas variants ar shēmu

The default storage model is structured storage. To override this behavior, and store the entire XML document as a single LOB column, use the STORE AS CLOB clause.

create table GRAMATVEIKALS_C of XMLType XMLTYPE STORE AS CLOB XMLSCHEMA "http://xmlns.oracle.com/xdb/schemas/SYSTEM/localhost:8080/public/gramatveikals.xsd" ELEMENT "gramatveikals"; insert into GRAMATVEIKALS_C values (XMLType ('<?xml version="1.0" encoding="UTF-8"?><!-- New document created with EditiX at Fri Dec 02 17:23:04 EET 2011 --><gramatveikals> <gramata kategorija="Pavārgrāmatas"> <virsraksts valoda="latviešu">Biezpiena sacepums</virsraksts> <autors>Juris Kālis</autors> <gads>2005</gads> <cena>15.00</cena> </gramata> <gramata kategorija="Daiļliteratūra"> <virsraksts valoda="latviešu">Biezais sniegs</virsraksts> <autors>Anita Zemniece</autors> <gads>2008</gads> <cena>10.00</cena> </gramata></gramatveikals>'));1 row created.

16

Automātiska XML shēmas ģenerēšana no relāciju-objektu tipa

1. An XML schema can be generated from an object-relational type automatically using a default mapping. PL/SQL functions generateSchema and generateSchemas in package DBMS_XMLSCHEMA take in a string that has the object type name and another that has the Oracle XML DB XML schema.1) function generateSchema returns an XMLType containing an XML schema. It can optionally generate an XML schema for all types referenced by the given object type or restricted only to the top-level types.2) function generateSchemas is similar, except that it returns an XMLSequenceType value. This is a varray of XMLType instances, each of which is an XML schema that corresponds to a different namespace. It also takes an additional optional argument that specifies the root URL of the preferred XML schema location:http://xmlns.oracle.com/xdb/schemas/<schema>.xsd

create type DARBINIEKS_TIPS as object(NUM number,UZV varchar2(200),ALGA number(8,2));

select DBMS_XMLSCHEMA.generateschema('SYSTEM', 'DARBINIEKS_TIPS') from DUAL;

DBMS_XMLSCHEMA.GENERATESCHEMA('SYSTEM','DARBINIEKS_TIPS')-------------------------------------------------------------------------------------------------------<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xdb="http://xmlns.oracle.com/xdb" xsi:schemaLocation="http://xmlns.oracle.com/xdb http://xmlns.oracle.com/xdb/XDBSchema.xsd"> <xsd:element name="DARBINIEKS_TIPS" type="DARBINIEKS_TIPSType" xdb:SQLType="DARBINIEKS_TIPS" xdb:SQLSchema="SYSTEM"/> <xsd:complexType name="DARBINIEKS_TIPSType" xdb:SQLType="DARBINIEKS_TIPS" xdb:SQLSchema="SYSTEM" xdb:maintainDOM="false"> <xsd:sequence> <xsd:element name="NUM" type="xsd:double" xdb:SQLName="NUM" xdb:SQLType="NUMBER"/> <xsd:element name="UZV" xdb:SQLName="UZV" xdb:SQLType="VARCHAR2"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:maxLength value="200"/> </xsd:restriction> </xsd:simpleType>

17

</xsd:element> <xsd:element name="ALGA" type="xsd:double" xdb:SQLName="ALGA" xdb:SQLType="NUMBER"/> </xsd:sequence> </xsd:complexType></xsd:schema>

18

Local XML Schema

1. XML schemas can be registered as local or global:1) a local xml schema is, by default, visible only to its owner;2) a global xml schema is, by default, visible and usable by all database users.

2. When you register an XML schema, PL/SQL package DBMS_XMLSCHEMA adds a corresponding resource to Oracle XML DB Repository. The XML schema URL determines the path name of the XML schema resource in the repository (and it is associated with the SCHEMAURL parameter of registerSchema).In Oracle Enterprise Manager, local and global registered XML schemas are referred to as private and public, respectively.

3. By default, an XML schema belongs to you after you register it with Oracle XML DB. A reference to the XML schema document is stored in Oracle XML DB Repository. Such XML schemas are referred to as local. By default, they are usable only by you, the owner. In Oracle XML DB, local XML schema resources are created under folder /sys/schemas/username. The rest of the repository path name is derived from the schema URL.beginDBMS_XMLSCHEMA.registerSchema(SCHEMAURL => 'http://xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd',SCHEMADOC => bfilename('XMLDIR','purchaseOrder.xsd'),LOCAL => TRUE,GENTYPES => TRUE, GENTABLES => FALSE, CSID => nls_charset_id('AL32UTF8'));end;

If this local XML schema is registered by user JANIS, it is given this path name:

/sys/schemas/JANIS/xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd

Database users need appropriate permissions and Access Control Lists (ACLs) to create a resource with this path name, in order to register the XML schema as a local XML schema.

Typically, only the owner of the XML schema can use it to define XMLType tables, columns, or views, validate documents, and so on. However, Oracle XML DB supports fully qualified XML schema URLs. For example: http://xmlns.oracle.com/xdb/schemas/JANIS/xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd. Privileged users can use such an extended URL to specify XML schemas belonging to other users.

19

Global XML Schema

1. A privileged user can register an XML schema as global by specifying an argument in the DBMS_XMLSCHEMA registration function. Global XML schemas are visible to all users. They are stored under folder /sys/schemas/PUBLIC/ in Oracle XML DB Repository.Note:Access to folder /sys/schemas/PUBLIC is controlled by access control lists (ACLs). By default, this folder is writable only by a database administrator. You need write privileges on this folder to register global XML schemas. Role XDBADMIN provides write access to this folder, assuming that it is protected by the default ACLs. You can register a local schema with the same URL as an existing global schema. A local schema always shadows (hides) any global schema with the same name (URL).

grant XDBADMIN to JANIS;connect JANISEnter password: parole

beginDBMS_XMLSCHEMA.registerSchema(SCHEMAURL => 'http://xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd',SCHEMADOC => bfilename('XMLDIR','purchaseOrder.xsd'),LOCAL => FALSE,GENTYPES => TRUE, GENTABLES => FALSE, CSID => nls_charset_id('AL32UTF8'));end;

If this global XML schema is registered by user JANIS, it is given this path name:

/sys/schemas/PUBLIC/xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd

20

Managing and Storing XML Schemas

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. The root XML schema describes any valid XML schema that can be registered with Oracle XML DB. You can access XDBSchema.xsd at Oracle XML DB Repository location /sys/schemas/PUBLIC/xmlns.oracle.com/xdb/XDBSchema.xsd.

For XML data stored object-relationally, you can monitor the object types and tables created during XML schema registration by setting the following event before calling DBMS_XMLSCHEMA.registerSchema:

ALTER SESSION SET EVENTS = '31098 trace name context forever'

Setting this event causes the generation of a log of all of the CREATE TYPE and CREATE TABLE statements. This log is written to the user session trace file, typically found in ORACLE_BASE/diag/rdbms/ORACLE_SID/ORACLE_SID/udump.

This script can be a useful aid in diagnosing problems during XML schema registration.

21

If parameter GENTYPES is TRUE when an XML schema is registered for use with XML data stored object-relationally, then Oracle XML DB creates the appropriate SQL object types that enable structured storage of conforming XML documents. By default, all SQL object types are created in the database schema of the user who registers the XML schema. If the defaultSchema annotation is used, then Oracle XML DB attempts to create the object type using the specified database schema. The current user must have the necessary privileges to create these object types.

Example shows the SQL object types that are created automatically when XML schema purchaseOrder.xsd is registered with Oracle XML DB.

DESCRIBE "PurchaseOrderType1668_T" "PurchaseOrderType1668_T" is NOT FINALName Null? Type--------------------------------------------------------------------------------SYS_XDBPD$ XDB.XDB$RAW_LIST_TReference VARCHAR2(30 CHAR)Actions ActionsType1661_TReject RejectionType1660_TRequestor VARCHAR2(128 CHAR)User VARCHAR2(10 CHAR)CostCenter VARCHAR2(4 CHAR)ShippingInstructions ShippingInstructionsTyp1659_TSpecialInstructions VARCHAR2(2048 CHAR)LineItems LineItemsType1666_TNotes VARCHAR2(4000 CHAR) DESCRIBE "LineItemsType1666_T" "LineItemsType1666_T" is NOT FINALName Null? Type---------------------------------------------------------------------SYS_XDBPD$ XDB.XDB$RAW_LIST_TLineItem LineItem1667_COLL

22

DESCRIBE "LineItem1667_COLL"

"LineItem1667_COLL" VARRAY(2147483647) OF LineItemType1665_T"LineItemType1665_T" is NOT FINALName Null? Type---------------------------------------------------------------------------SYS_XDBPD$ XDB.XDB$RAW_LIST_TItemNumber NUMBER(38)Description VARCHAR2(256 CHAR)Part PartType1664_T

Default Tables Created During XML Schema RegistrationAs part of XML schema registration for XML data, you can create default tables. Default tables are most useful when documents conforming to the XML schema are inserted through APIs and protocols such as FTP and HTTP(S) that do not provide any table specification. In such cases, the XML instance is inserted into the default table.Example 6-5 Default Table for Global Element PurchaseOrderDESCRIBE "purchaseorder1669_tab"

Name Null? Type--------------------------- ----- -----------------------TABLE of SYS.XMLTYPE( XMLSchema "http://xmlns.oracle.com/xdb/documentation/purchaseOrder.xsd" Element "PurchaseOrder") STORAGE OBJECT-RELATIONAL TYPE "PurchaseOrderType1668_T"If you provide a value for attribute defaultTable, then the XMLType table is created with that name. Otherwise it is created with an internally generated name.Any text specified using the tableProps and columnProps attributes is appended to the generated CREATE TABLE statement.Generated Names are Case SensitiveThe names of any SQL tables, object, and attributes generated by XML schema registration are case sensitive. For instance, in Example 6-3, a table named PurchaseOrder1669_TAB is created automatically during registration of the XML schema. Since this table name was derived from the element name, PurchaseOrder, the table name is also mixed case. This means that you must refer to this table in SQL code by using a quoted identifier: "PurchaseOrder1669_TAB". Failure to do so results in an object-not-found error, such as ORA-00942: table or view does not exist.Database Objects That Depend on Registered XML SchemasThe following database objects are dependent on registered XML schemas: Tables or views that have an XMLType column that conforms to an element in an XML schema.

23

Other XML schemas that include or import a given XML schema as part of their definition. Cursors that reference an XML schema. This includes references within functions of package DBMS_XMLGEN. Such cursors are purely transient objects.

24

XMLType Methods Related to XML Schema

XMLType Metode AprakstsisSchemaBased() Returns TRUE if the XMLType instance is based on an XML

schema, FALSE otherwise.getSchemaURL() getRootElement()getNamespace()

The XML schema URL, root-element name, and namespace, respectively, for an XMLType instance. Method getRootElement() can be used with non-schema-based data, as well as with XML schema-based data. Note that, in spite of its name, getRootElement() does not return the root element; it returns the element name.

schemaValidate()isSchemaValid()isSchemaValidated()setSchemaValidated()

Validate an XMLType instance against a registered XML schema.

Specifying Relational Constraints on XMLType Tables and ColumnsWhen you store XML data using structured storage, typical relational constraints can be specified for elements and attributes that occur only once in an XML document.

25