在Oracle PL / SQL中针对模式验证XML文件

时间:2008-09-17 11:08:05

标签: xml oracle ora-06512

我需要针对XSD验证传入文件。两者都将在服务器文件系统上。

我查看了dbms_xmlschema,但在让它发挥作用方面遇到了问题。

使用Java可以更容易吗?
我可以在数据库中放入哪个最简单的类?

这是一个简单的例子:

DECLARE
  v_schema_url       VARCHAR2(200) := 'http://www.example.com/schema.xsd';
  v_blob             bLOB;
  v_clob             CLOB;
  v_xml XMLTYPE;
BEGIN
  begin
    dbms_xmlschema.deleteschema(v_schema_url);
  exception
    when others then
     null;
  end;

  dbms_xmlschema.registerSchema(schemaURL => v_schema_url,
                                schemaDoc => '
<xs:schema targetNamespace="http://www.example.com" 
xmlns:ns="http://www.example.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified" version="3.0">
<xs:element name="something"  type="xs:string"/>
</xs:schema>',
                                local => TRUE);

  v_xml := XMLTYPE.createxml('<something xmlns="http://www.xx.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.com/schema.xsd">
data
</something>');

  IF v_xml.isschemavalid(v_schema_url) = 1 THEN
    dbms_output.put_line('valid');
  ELSE
    dbms_output.put_line('not valid');
  END IF;
END;

这会产生以下错误:

ORA-01031: insufficient privileges
ORA-06512: at "XDB.DBMS_XDBZ0", line 275
ORA-06512: at "XDB.DBMS_XDBZ", line 7
ORA-06512: at line 1
ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 3
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 14
ORA-06512: at line 12

6 个答案:

答案 0 :(得分:5)

<强>更新

XML Schema注册需要以下权限:

grant alter session to <USER>;
grant create type to <USER>; /* required when gentypes => true */
grant create table to <USER>; /* required when gentables => true */

由于某些原因,如果通过角色间接授予这些权限是不够的,但是权限需要直接授予模式/用户

原始答案

我还注意到参数gentablesgentypes的默认值会引发insufficient privileges异常。可能我只是缺乏使用这些功能的一些特权,但目前我还没有很好地理解他们做了什么。我很高兴禁用它们,验证似乎工作正常。

我在Oracle Database 11g第11.2.0.1.0版上运行

gentypes =&gt;是的,gentables =&gt;真

dbms_xmlschema.registerschema(schemaurl => name,
                              schemadoc => xmltype(schema),
                              local => true
                              --gentypes => false,
                              --gentables => false
                              );

ORA-01031: insufficient privileges
ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 55
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 159
ORA-06512: at "JANI.XML_VALIDATOR", line 38
ORA-06512: at line 7

gentypes =&gt; false,gentables =&gt;真

dbms_xmlschema.registerschema(schemaurl => name,
                              schemadoc => xmltype(schema),
                              local => true,
                              gentypes => false
                              --gentables => false
                              );

ORA-31084: error while creating table "JANI"."example873_TAB" for element "example"
ORA-01031: insufficient privileges
ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 55
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 159
ORA-06512: at "JANI.XML_VALIDATOR", line 38
ORA-06512: at line 7

gentypes =&gt;是的,gentables =&gt;假

dbms_xmlschema.registerschema(schemaurl => name,
                              schemadoc => xmltype(schema),
                              local => true,
                              --gentypes => false
                              gentables => false
                              );

ORA-01031: insufficient privileges
ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 55
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 159
ORA-06512: at "JANI.XML_VALIDATOR", line 38
ORA-06512: at line 7

gentypes =&gt; false,gentables =&gt;假

dbms_xmlschema.registerschema(schemaurl => name,
                              schemadoc => xmltype(schema),
                              local => true,
                              gentypes => false,
                              gentables => false
                              );

PL/SQL procedure successfully completed.

答案 1 :(得分:2)

您必须拥有ALTER SESSION权限才能注册架构。

答案 2 :(得分:2)

这是一段适合我的代码。 user272735的答案是对的,我写了另一个答案,因为我无法在评论中写出所有代码(太长时间)。

/* Formatted on 21/08/2012 12:52:47 (QP5 v5.115.810.9015) */
DECLARE
   -- Local variables here
   res          BOOLEAN;
   tempXML      XMLTYPE;
   xmlDoc       XMLTYPE;
   xmlSchema    XMLTYPE;
   schemaURL    VARCHAR2 (256) := 'testcase.xsd';
BEGIN
   dbms_xmlSchema.deleteSchema (schemaURL, 4);
   -- Test statements here
   xmlSchema :=
      xmlType('<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root" xdb:defaultTable="ROOT_TABLE">
<xs:complexType>
<xs:sequence>
<xs:element name="child1"/>
<xs:element name="child2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
');
    -- http://stackoverflow.com/questions/82047/validating-xml-files-against-schema-in-oracle-pl-sql
    dbms_xmlschema.registerschema(schemaurl => schemaURL,
                                  schemadoc => xmlSchema,
                                  local => true,
                                  gentypes => false,
                                  gentables => false
                                  );
   xmlDoc :=
      xmltype('<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="'
              || schemaURL
              || '"><child1>foo</child1><child2>bar</child2></root>');
   xmlDoc.schemaValidate ();
   -- if we are here, xml is valid
   DBMS_OUTPUT.put_line ('OK');
exception
    when others then
    DBMS_OUTPUT.put_line (SQLErrm);
END;

答案 3 :(得分:0)

一旦您解决了安装问题,当模式变得很大时,某些Oracle版本就会遇到挑战,特别是当您拥有包含其他模式的模式时。我知道我们在9.2中有这个问题,不确定10.2或11。

对于像你的例子这样的小型模式,它应该可以正常工作。

答案 4 :(得分:0)

注册XSD会导致创建表,类型和触发器。因此,您需要以下拨款:

grant create table to <user>;
grant create type to <user>;
grant create trigger to <user>;

答案 5 :(得分:-2)

如果我没记错的话,在没有正确安装XDB(Oracle的XML DataBase包)时会给出错误消息。让DBA检查一下。