Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: schema Validating XML

Re: schema Validating XML

From: Michael O'Shea <michael.oshea_at_tessella.com>
Date: 11 Oct 2005 09:36:49 -0700
Message-ID: <1129048609.850522.19990@g47g2000cwa.googlegroups.com>


Hi there, the key functionality you are after is achieved through i) registering the XML schema and ii) validating the XML application against the schema using the XMLTYPE.SCHEMAVALIDATE method. I have posted a few times to this newsgroup on XMLDB things and replied to people offline. Here is an example script I have used previously to demonstrate how to both register and validate XML within Oracle. It is not exactly what you require but will certainly point you in the right direction with a working example. What you will be doing is loading XML into a variable of XMLTYPE and then validating against a registered schema. The Oracle doc you should download is "Oracle XMLDB, Developers Guide", part number B14259-02.

Others in this newsgroup may respond to this post that you should not be performing expensive XML validation on the enterprise server (unless there is a <b>very</b> good reason for doing so). In advance I agree. You state "I want to validate this xmls in the start of every function/procedures to make sure that...". You really might want to reconsider this requirement.

Kind regards
Mike

TESSELLA Michael.OShea_at_tessella.com

__/__/__/  Tessella Support Services plc
__/__/__/  3 Vineyard Chambers, ABINGDON, OX14 3PX, England
__/__/__/  Tel: (44)(0)1235-555511  Fax: (44)(0)1235-553301
www.tessella.com Registered in England No. 1466429

SQL>
SQL> DECLARE
  2 schema VARCHAR2(1000) := '
  3 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

  4               xmlns:xdb="http://xmlns.oracle.com/xdb">
  5      <xs:element name="Employee">
  6       <xs:complexType xdb:SQLType="EMPLOYEEDETAIL_T">
  7        <xs:sequence>
  8         <xs:element name="EmployeeID" type="xs:integer"
nillable="false"/>
  9         <xs:element name="Gender" type="RestrictionGender"
nillable="false"/>
 10         <xs:element name="HomeAddress" nillable="false"
maxOccurs="unbounded">
 11          <xs:complexType>
 12           <xs:sequence>
 13            <xs:element name="Postcode" type="xs:string"
nillable="false"/>
 14            <xs:element name="HouseNumber" type="xs:integer"
nillable="false"/>
 15           </xs:sequence>
 16          </xs:complexType>
 17         </xs:element>
 18        </xs:sequence>
 19       </xs:complexType>
 20     </xs:element>
 21     <xs:simpleType name="RestrictionGender">
 22      <xs:restriction base="xs:string">
 23       <xs:enumeration value="male"/>
 24       <xs:enumeration value="female"/>
 25      </xs:restriction>
 26     </xs:simpleType>

 27 </xs:schema>
 28 ';
 29 BEGIN
 30 BEGIN
 31
DBMS_XMLSCHEMA.DELETESCHEMA('http://www.tessella.co.uk/employee',  32
DBMS_XMLSCHEMA.DELETE_CASCADE_FORCE);
 33     EXCEPTION
 34      WHEN OTHERS THEN
 35        NULL;

 36 END;
 37
DBMS_XMLSCHEMA.REGISTERSCHEMA('http://www.tessella.co.uk/employee',schema,TRUE,TRUE,FALSE,FALSE);  38
DBMS_XMLSCHEMA.COMPILESCHEMA('http://www.tessella.co.uk/employee');  39 END;
 40 /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> CREATE TABLE tblTest(id NUMBER PRIMARY KEY,
  2                       details XMLTYPE NOT NULL
  3                      ) XMLTYPE COLUMN details
  4                                          XMLSCHEMA
"http://www.tessella.co.uk/employee"
  5                                           ELEMENT "Employee";

Table created.

SQL>
SQL> CREATE OR REPLACE TRIGGER trgTest
  2 BEFORE INSERT OR UPDATE OR DELETE ON tblTest   3 FOR EACH ROW
  4 DECLARE
  5 xml XMLTYPE;
  6 BEGIN
  7 xml:=:NEW.details;
  8 IF INSERTING OR UPDATING THEN
  9 xml.SCHEMAVALIDATE();
 10 ELSIF DELETING THEN
 11 NULL;
 12 END IF;
 13 END;
 14 /

Trigger created.

SQL> --Both schema valid and well formed XML SQL> INSERT INTO tblTest(id,details)
  2 VALUES(1,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>

  3                     <Employee

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   4
xsi:noNamespaceSchemaLocation="http://www.tessella.co.uk/employee">
  5                      <EmployeeID>1</EmployeeID>
  6                      <Gender>male</Gender>
  7                      <HomeAddress>
  8                       <Postcode>OX11 0RT</Postcode>
  9                       <HouseNumber>100</HouseNumber>
 10                      </HomeAddress>
 11                     </Employee>'
 12                   )
 13         );

1 row created.

SQL> --Well formed XML but not schema valid (gender=man and not male) SQL> INSERT INTO tblTest(id,details)
  2 VALUES(2,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>

  3                     <Employee

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   4
xsi:noNamespaceSchemaLocation="http://www.tessella.co.uk/employee">
  5                      <EmployeeID>2</EmployeeID>
  6                      <Gender>man</Gender>
  7                      <HomeAddress>
  8                       <Postcode>OX11 0RT</Postcode>
  9                       <HouseNumber>101</HouseNumber>
 10                      </HomeAddress>
 11                     </Employee>'
 12                   )
 13         );
 VALUES(2,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
          *

ERROR at line 2:
ORA-31038: Invalid enumeration value: "man"

SQL> --Well formed XML but not schema valid (no element HouseName defined in XML schema)
SQL> INSERT INTO tblTest(id,details)
  2 VALUES(3,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>

  3                     <Employee

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   4
xsi:noNamespaceSchemaLocation="http://www.tessella.co.uk/employee">
  5                      <EmployeeID>2</EmployeeID>
  6                      <HomeAddress>
  7                       <Postcode>OX11 0RT</Postcode>
  8                       <HouseName>The Gables</HouseName>
  9                      </HomeAddress>
 10                     </Employee>'
 11                   )
 12         );
 VALUES(3,XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
          *

ERROR at line 2:
ORA-30937: No schema definition for 'HouseName' (namespace '##local') in parent
'HomeAddress'
SQL>
SQL>
SQL>
SQL> SELECT *

  2 FROM V$VERSION; BANNER

Personal Oracle Database 10g Release 10.1.0.2.0 - Production PL/SQL Release 10.1.0.2.0 - Production
CORE 10.1.0.2.0 Production
TNS for 32-bit Windows: Version 10.1.0.2.0 - Production NLSRTL Version 10.1.0.2.0 - Production

SQL> SPOOL OFF tal fts wrote:
> Hi
>
> i have functions/procedures which gets xml documents as paramters.
> i want to validate this xmls in the start of every function/procedures
> to make sure that all tags will be found when i run my functions.
>
> i saw that this could be done using schema documents.
> i want to store these xml schema documents in a table, one for each
> type of xml i get in one of the function.
> then i want to retrieve this schema and validate the xml against this
> schema.
> i read somewhere this could be done using dburi types, but it didnt
> gave any example. (do u have one?)
>
> i saw that u can reference a table with a schema and then do full xml
> validation in a trigger. i dont need this behaviour as i dont insert
> data into tables, just need the verify that the xml is in the correct
> form.
>
> is there a simple way in PL/SQL to load an xml and validate it againt a
> schema.
> an exmaple will be great.
Received on Tue Oct 11 2005 - 11:36:49 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US