2016-03-19 9 views
2

Ich versuche ein Dokument XML zu validieren, während es in eine Tabelle in der ORACLE-Datenbank eingefügt wird. Ich habe ein XML-Schema und ein XMLTYPE mit diesem Schema in meiner Tabelle definiert, aber die Datenbank erlaubt mir, ein falsches XML einzufügen.Oracle DB, validiere XML während des Einfügens

Mein Schema:

BEGIN 
dbms_xmlschema.registerschema(
    schemaurl => 'http://www.testXml.com/schema.xsd', 
    schemadoc => xmltype('<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:complexType name="Name_tp"> 
    <xs:simpleContent> 
    <xs:extension base="xs:string"> 
     <xs:attribute name="Id" type="xs:positiveInteger"/> 
    </xs:extension> 
    </xs:simpleContent> 
</xs:complexType> 
<xs:complexType name="Category_tp"> 
    <xs:attribute name="NumPrize" type="xs:positiveInteger"/> 
    <xs:attribute name="From" type="xs:integer"/> 
    <xs:attribute name="To" type="xs:positiveInteger"/> 
    <xs:attribute name="Type" type="xs:string"/> 
    <xs:attribute name="Age" type="xs:positiveInteger"/> 
</xs:complexType> 
<xs:complexType name="Match_tp"> 
    <xs:attribute name="typeMatch" type="xs:string"/> 
</xs:complexType> 
<xs:complexType name="GolfCompetition_tp"> 
    <xs:sequence> 
    <xs:element name="Name" type="Name_tp"/> 
    <xs:element name="Date" type="xs:date"/> 
    <xs:element name="Sponsor" type="xs:string"/> 
    <xs:element name="Category" maxOccurs="unbounded" type="Category_tp"/> 
    <xs:element name="Reserved" type="Match_tp" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 
    <xs:element name="GolfCompetition" type="GolfCompetition_tp"/> 
</xs:schema>'), 
local => true, 
gentypes => false, 
gentables => false 
); 
END; 

Mein db Schema:

CREATE TYPE t_gara AS OBJECT (
    id INTEGER, 
    informazioni XMLTYPE 
); 

CREATE TABLE gara OF t_gara() XMLType COLUMN informazioni 
XMLSCHEMA "http://www.testXml.com/schema.xsd" 
ELEMENT "GolfCompetition"; 

Wenn ich versuche, diese Zeile einzufügen ich habe nicht Problem:

INSERT INTO GARA VALUES(1, XMLType('<GolfCompetition> 
<Name Id="324">Coppa del Presidente</Name> 
<Date>2009-12-25</Date> 
<Sponsor>Lavazza S.p.A</Sponsor> 
<Category NumPrize="3" From="0" To="12" Type="First"/> 
<Category NumPrize="3" From="13" To="24" Type="Second"/> 
<Category NumPrize="2" From="25" To="36" Type="Third"/> 
<Category NumPrize="1" Type="Lady"/> 
<Category NumPrize="1" Type="Over" Age="40"/> 
</GolfCompetition>')); 

Das funktioniert aber zu:

INSERT INTO GARA VALUES(2, XMLType('<GolfCompetition> 
<Category NumPrize="3" From="0" To="12" Type="First"/> 
<Category NumPrize="3" From="13" To="24" Type="Second"/> 
<Category NumPrize="2" From="25" To="36" Type="Third"/> 
<Category NumPrize="1" Type="Lady"/> 
<Category NumPrize="1" Type="Over" Age="40"/> 
</GolfCompetition>')); 

Wie kann ich lösen?

Antwort

2

Die automatische Schemavalidierung erfordert das Speichern des XML in einem binären Format, nicht objektbezogen.

Von der XML Developer's Guide:

For XMLType data that is stored object-relationally, full validation requires building a DOM, which can be costly in terms of memory management. For this reason, Oracle XML DB does not automatically perform full validation when you insert or update data that is stored object-relationally.

Re-register das Schema, das Hinzufügen dieser Parameter:

... 
options => dbms_xmlschema.REGISTER_BINARYXML 
... 

Erstellen Sie die Tabelle wie folgt aus:

create table gara 
(
    id number, 
    informazioni xmltype 
) xmltype informazioni 
    store as binary xml 
    XMLSCHEMA "http://www.testXml.com/schema.xsd" 
    ELEMENT "GolfCompetition"; 

Nun ist der erste Einsatz wird funktionieren, aber die zweite wird mit diesem Fehler fehlschlagen:

ORA-64464: XML event error 
ORA-19202: Error occurred in XML processing 
LSX-00213: only 0 occurrences of particle "Name", minimum is 1 
Verwandte Themen