|
Page 1 of 2
Using XML Schemas
The simplest way to create an XMLType storage structure in Oracle XML DB is by registering an appropriate XML schema against the database. As a part of the registration process, Oracle automatically creates the storage for a particular set of XML documents, based on the information provided by the schema.
NOTE: An XML schema can be thought of as the metadata describing a certain class of XML documents. So, an XML document conforming to a particular XML schema can be considered as an instance of this XML schema.
You might want to use an XML schema for:
- Building the storage for XML documents conforming the schema
- Setting up business rules on XML content of conforming documents
- Validating XML documents conforming to the schema
However, before you can use an XML schema, you have to create and then register it against the database. Both these tasks can be accomplished in one step with the registerschema procedure from the DBMS_XMLSCHEMA PL/SQL package. For example, to register an XML schema to which the following employee XML document conforms:
King
24000
You might issue the following statements:
CONN /AS sysdba GRANT ALTER SESSION TO xmlusr; CONN xmlusr/xmlusr BEGIN DBMS_XMLSCHEMA.registerschema( 'employee.xsd', '<xs:schema xmlns:xdb="http://xmlns.oracle.com/xdb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element type="EMPLOYEE_TYP" name="EMPLOYEE" xdb:defaulttable="EMPLOYEES" xdb:columnprops="CONSTRAINT emp_pkey PRIMARY KEY (XMLDATA.empno)"> </xs:element> <xs:complextype name="EMPLOYEE_TYP" xdb:sqltype="EMPLOYEE_T"> <xs:sequence> <xs:element type="xs:string" name="ENAME" xdb:sqltype="VARCHAR2" xdb:sqlname="ENAME"> </xs:element> <xs:element type="xs:double" name="SALARY" xdb:sqltype="NUMBER" xdb:sqlname="SALARY"> </xs:element> </xs:sequence> <xs:attribute type="xs:positiveInteger" name="id" xdb:sqltype="NUMBER" xdb:sqlname="EMPNO"> </xs:attribute> </xs:complextype> </xs:schema>', TRUE, TRUE, FALSE, TRUE ); END; /
%20%20%20%20%20CONN%20%2Fas%20sysdba%0D%0A%0D%0A%20%20%20%20%20GRANT%20ALTER%20SESSION%20TO%20xmlusr%3B%0D%0A%0D%0A%20%20%20%20%20CONN%20xmlusr%2Fxmlusr%0D%0A%0D%0A%20%20%20%20%20BEGIN%0D%0A%20%20%20%20%20%20%20%20%20%20DBMS_XMLSCHEMA.registerschema%28%0D%0A%20%20%20%20%20%20%20%20%20%20%27employee.xsd%27%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%27%3Cxs%3Aschema%20xmlns%3Axdb%3D%22http%3A%2F%2Fxmlns.oracle.com%2Fxdb%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xmlns%3Axs%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Aelement%20type%3D%22EMPLOYEE_TYP%22%20name%3D%22EMPLOYEE%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20xdb%3Adefaulttable%3D%22EMPLOYEES%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20xdb%3Acolumnprops%3D%22CONSTRAINT%20emp_pkey%20PRIMARY%20KEY%20%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%28XMLDATA.empno%29%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Aelement%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Acomplextype%20name%3D%22EMPLOYEE_TYP%22%20xdb%3Asqltype%3D%22EMPLOYEE_T%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Asequence%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Aelement%20type%3D%22xs%3Astring%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22ENAME%22%20xdb%3Asqltype%3D%22VARCHAR2%22%20xdb%3Asqlname%3D%22ENAME%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Aelement%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Aelement%20type%3D%22xs%3Adouble%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20name%3D%22SALARY%22%20xdb%3Asqltype%3D%22NUMBER%22%20xdb%3Asqlname%3D%22SALARY%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Aelement%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Asequence%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3Cxs%3Aattribute%20type%3D%22xs%3ApositiveInteger%22%20%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22id%22%20xdb%3Asqltype%3D%22NUMBER%22%20xdb%3Asqlname%3D%22EMPNO%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Aattribute%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fxs%3Acomplextype%3E%0D%0A%20%20%20%20%20%3C%2Fxs%3Aschema%3E%27%2C%0D%0A%20%20%20%20%20TRUE%2C%0D%0A%20%20%20%20%20TRUE%2C%0D%0A%20%20%20%20%20FALSE%2C%0D%0A%20%20%20%20%20TRUE%0D%0A%20%20%20%20%29%3B%0D%0A%20%20END%3B%0D%0A%2F
As you can see, the DBMS_XMLSCHEMA.registerschema procedure takes two arguments. The first one is the string representing the name under which you want to register the schema against the database, and the other one is the document containing the schema itself.
In this example, the root element of the XML schema includes two namespace declarations, namely the XML schema namespace declaration and Oracle XML DB namespace declaration. To denote these namespaces, you use prefixes: xs and xdb respectively.
By including the XML schema namespace declaration, you obtain the ability to use the elements and attributes defined in this namespace, as well as the data types defined by the XML Schema language. For example, in the above example you specify the positiveInteger XML Schema language data type for the id attribute of the EMPLOYEE element.
The Oracle XML DB namespace lets you use annotations in the schema. For example, you use the xdb:defaultTable annotation to tell Oracle to use the specified table name when generating an XMLType table that will be used for storing XML documents conforming to the schema, rather than using a system-generated name for that table. In this particular example, you specify EMPLOYEES as the name for this table.
Another interesting annotation used in this XML schema is the xdb:columnProps. In this example, you use this annotation to defi ne a primary key on the EMPLOYEE element's id attribute mapped to the EMPNO attribute of the EMPLOYEE_T SQL object type.
By including the xdb:SQLName annotation you make sure that the name of the generated SQL object type will be EMPLOYEE_T.
Finally, note the use of the flags passed to the DBMS_XMLSCHEMA.registerschema procedure:
TRUE,
TRUE,
FALSE,
TRUE
The above flags indicate the following (in the same order as they appear in the listing):
- The schema is generated as local (visible only to the database user who created it)
- Appropriate SQL object types are generated
- Java beans are not generated
- Default tables are generated
After registering the schema, you might want to look at the database object generated during the registration. The following listing contains the SQL statements that you might issue from SQL*Plus to make sure that Oracle generated the objects annotated in the schema. For convenience, the listing also contains the output produced.

TABLE OF SYS.XMLTYPE( XMLSchema "employee.xsd" Element "EMPLOYEE") STORAGE Object-relational TYPE "EMPLOYEE_T"
%20%20%20%20%20TABLE%20of%0D%0A%26nbsp%3B%20%26nbsp%3BSYS.XMLTYPE%28%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3BXMLSchema%20%22employee.xsd%22%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3BElement%20%22EMPLOYEE%22%29%0D%0A%26nbsp%3B%20%26nbsp%3BSTORAGE%20Object-relational%20TYPE%20%22EMPLOYEE_T%22
As you can see, Oracle generated the employee_t object type and employees XMLType table based on this object type, as a part of the XML schema registration process. Note that the names of the generated objects have been defined in the schema. If you recall, you set the value of the xdb:SQLName attribute of global element EMPLOYEE to EMPLOYEE_T, and the xdb:defaultTable attribute to EMPLOYEES.
NOTE: It's interesting to note that the names of database objects generated during the XML schema registration process are case sensitive. However, since SQL is case insensitive, you can refer to these objects in SQL disregarding the case of their names. The names of XML elements and attributes specified in an XML schema are also case sensitive. However, unlike SQL, XML is case-sensitive, which means you must refer to XML elements and attributes in XML code using the case with which they were defined in the XML schema.
|