2

我正在尝试在将文档 XML 插入 ORACLE 数据库中的表期间对其进行验证。我已经在我的表中定义了一个 XML Schema 和一个带有此模式的 XMLTYPE,但是 db 允许我插入错误的 xml。

我的架构:

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;

我的数据库架构:

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";

当我尝试插入这一行时,我没有问题:

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>'));

但这适用于:

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>'));

我该如何解决?

4

1 回答 1

2

自动模式验证需要以二进制格式存储 XML,而不是对象关系。

XML 开发人员指南

对于以对象关系方式存储的 XMLType 数据,完全验证需要构建一个 DOM,这在内存管理方面可能代价高昂。因此,当您插入或更新以对象关系方式存储的数据时,Oracle XML DB 不会自动执行完全验证。

重新注册架构,添加此参数:

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

像这样创建表:

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

现在第一个插入将起作用,但第二个将失败并出现以下错误:

ORA-64464: XML event error
ORA-19202: Error occurred in XML processing
LSX-00213: only 0 occurrences of particle "Name", minimum is 1
于 2016-03-20T03:53:27.680 回答