0% found this document useful (0 votes)
9 views7 pages

dbms10 (1) 38

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

dbms10 (1) 38

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.No : 10 Create an XML database and validate it using XML schema.

AIM
To Create an XML database and validate it using XML schema.

First we need to register the schema using


the DBMS_XMLSCHEMA.REGISTERSCHEMA procedure. This has a number of overloads, allowing
you to specify the XML schema using
a VARCHAR2, BFILE, BLOB, CLOB, XMLTYPE or URIType types. The parameter list is
quite important. By default the REGISTERSCHEMA procedure will create database types and
object tables, allowing documents to be shredded into object tables. For complex XSD
documents you might get thousands of objects created in your database schema. For a basic
document validation this is unnecessary and messy, so check you are using the correct
settings.

DECLARE
l_schema CLOB;
BEGIN

l_schema := '<?xml version="1.0" encoding="UTF-8" ?>


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements -->


<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes -->


<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->


<xs:element name="shipto">
<xs:complexType>
<xs:sequence> 721222104031
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>';

DBMS_XMLSCHEMA.registerSchema(schemaurl => 'my_schema.xsd',


schemadoc => l_schema,
local => TRUE,
gentypes => FALSE,
gentables => FALSE,
enablehierarchy =>
DBMS_XMLSCHEMA.enable_hierarchy_none);
END;
/

We can check the schema details using the USER_XML_SCHEMAS view. 721222104031
------------------------------------------------------------------------------
--

With the schema registered, we can now validate XML documents against it.
The DELETESCHEMA procedure can be used to un-register the schema.
The DELETE_OPTION parameter allows you to drop any dependent objects.

BEGIN
DBMS_XMLSCHEMA.deleteschema(
schemaurl => 'my_schema.xsd',
delete_option => DBMS_XMLSCHEMA.delete_cascade_force);
END;
/

Validate XML Document (SCHEMAVALIDATE)


In the following PL/SQL example, we create an XMLTYPE from some XML in a CLOB,
then call the SCHEMAVALIDATE member procedure to test the XML against the XML
schema.

DECLARE
l_xml CLOB;
l_xmltype XMLTYPE;
BEGIN

l_xml := '<?xml version="1.0" encoding="UTF-8"?>


<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
721222104031
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>';

l_xmltype := XMLTYPE(l_xml, 'my_schema.xsd');


l_xmltype.schemavalidate;

END;
/

To see an example of a validation failure, rename the "name" tag to "name1". This is
not part of the XML schema, so it will fail the validation.

DECLARE
l_xml CLOB;
l_xmltype XMLTYPE;
BEGIN

l_xml := '<?xml version="1.0" encoding="UTF-8"?>


<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name1>Ola Nordmann</name1>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
721222104031
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>';

l_xmltype := XMLTYPE(l_xml, 'my_schema.xsd');


l_xmltype.schemavalidate;

END;
/

Validate XML Document (XMLISVALID)


An alternative is to use the XMLISVALID function.
Create a table to hold the XML we used in the previous tests.

CREATE TABLE t1 (
id NUMBER,
xml XMLTYPE
);

INSERT INTO t1 VALUES (1, '<?xml version="1.0" encoding="UTF-8"?>


<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
721222104031
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>');

INSERT INTO t1 VALUES (2, '<?xml version="1.0" encoding="UTF-8"?>


<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name1>Ola Nordmann</name1>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>');

COMMIT;

Now we can use the XMLISVALID function in SQL to test against the registered XML
721222104031
Schema. It returns a "1" if the XML is valid, and "0" if it isn't.

---------- ----------

RESULT:
To Create the XML database and XML Schema are Validate Successfully.

721222104031

You might also like