根据 Oracle PL/SQL 中的模式验证 XML 文件

发布于 2024-07-04 07:16:03 字数 1580 浏览 15 评论 0原文

我需要根据 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

I have a requirement to validate an incoming file against an XSD. Both will be on the server file system.

I've looked at dbms_xmlschema, but have had issues getting it to work.

Could it be easier to do it with some Java?
What's the simplest class I could put in the database?

Here's a simple example:

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;

This generates the following error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

魄砕の薆 2024-07-11 07:16:03

更新

XML 架构注册需要以下权限:

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

出于某种原因,如果通过角色间接授予这些权限是不够的,但需要将这些权限直接授予架构/用户。

原始答案

我还注意到参数gentablesgentypes的默认值会引发权限不足异常。 可能我只是缺乏使用这些功能的一些权限,但目前我不太了解它们的作用。 我很高兴禁用它们,并且验证似乎工作正常。

我正在 Oracle Database 11g 版本 11.2.0.1.0 上运行

gentypes => 是的,绅士=> 真实

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

基因型=> 假,绅士=> 真实

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

基因型=> 是的,绅士=> 错误

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

基因型=> 假,绅士=> 错误的

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

PL/SQL procedure successfully completed.

Update

XML Schema registration requires following privileges:

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

For some reason it's not enough if those privileges are granted indirectly via roles, but the privileges need to be granted directly to schema/user.

Original Answer

I have also noticed that default values of parameters gentables and gentypes raise insufficient privileges exception. Probably I'm just lacking of some privileges to use those features, but at the moment I don't have a good understanding what they do. I'm just happy to disable them and validation seems to work fine.

I'm running on Oracle Database 11g Release 11.2.0.1.0

gentypes => true, gentables => true

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 => false, gentables => true

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 => true, gentables => false

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 => false, gentables => false

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

PL/SQL procedure successfully completed.
你与昨日 2024-07-11 07:16:03

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

You must have ALTER SESSION privilege granted in order to register a schema.

尛丟丟 2024-07-11 07:16:03

这是一段适合我的代码。
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;

here is a piece of code that works for me.
user272735's answer is right, I wrote another answer as far as I can't write all the code in a comment (too long).

/* 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;
樱娆 2024-07-11 07:16:03

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

不过,对于像您的示例这样的小型模式,它应该可以工作。

Once you get past the install issues, there are challenges in some Oracle versions when the schemas get big, particularly when you have schemas that include other schemas. I know we had that issue in 9.2, not sure about 10.2 or 11.

For small schemas like your example, though, it should just work.

二智少女猫性小仙女 2024-07-11 07:16:03

如果我没记错的话,当 XDB(Oracle 的 XML 数据库包)未正确安装时,会出现该错误消息。 让 DBA 检查一下。

If I remember correctly, that error message is given when XDB (Oracle's XML DataBase package) is not properly installed. Have the DBA check this out.

标点 2024-07-11 07:16:03

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

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

Registering the XSD leads to creation of tables, types and triggers. Therefore you need the following grants:

grant create table to <user>;
grant create type to <user>;
grant create trigger to <user>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文