xmltype column and schema validation broken on xs:choice elements

From: Piotr Swiecicki <swietopel_at_gmail.com>
Date: Fri, 31 Oct 2008 03:21:12 -0700 (PDT)
Message-ID: <a66c9e79-3750-440d-a5c1-9355657b37c2@d10g2000pra.googlegroups.com>

I have a problem with schema validation on table with xmltype column.
I have very simple schema: "X" root element with one of the "A" or "B" child.

  DBMS_XMLSCHEMA.registerSchema(schemaURL => '/schemas/test', schemaDoc =>
'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"


  <xs:element name="X">
        <xs:element name="A" type="xs:anyType"/>
        <xs:element name="B" type="xs:anyType"/>


  local => true);

I have a table as well with a xmltype column, bound to the above schema
create table xx_test (
  xml xmltype
 XMLSCHEMA "/schemas/test"
 ELEMENT "X"; Now when I am trying to insert a valid XML document, it works: SQL> -- expecting row inserted
SQL> insert into xx_test (xml) values ('<X><A/></X>'); 1 row inserted

But I am surprised this row is inserted as well: SQL> -- expecting row rejected
SQL> insert into xx_test (xml) values ('<X/>'); 1 row inserted

It seems the schema validation works, because if I try to insert a totally wrong document it is rejected:
SQL> -- expecting row rejected
SQL> insert into xx_test (xml) values ('<X><WRONG_ELEMENT/></X>'); ORA-30937: No schema definition for 'WRONG_ELEMENT' (namespace '##local') in parent '/X'

What is more interesting isSchemaValid member function of XMLType works as expected with the above documents: SQL> SELECT t.txt,
  2 decode(xmltype(t.txt).isschemavalid('/schemas/test', 'X'), 0, 'invalid', 'valid') v
  3 FROM (SELECT '<X><A/></X>' txt

  4            FROM dual
  5          UNION ALL
  6          SELECT '<X><WRONG_ELEMENT/></X>' txt
  7            FROM dual
  8          UNION ALL
  9          SELECT '<X/>' txt
 10            FROM dual) t;

TXT                     V
----------------------- -------

<X><A/></X> valid
<X><WRONG_ELEMENT/></X> invalid
<X/> invalid

Can you help me please?

Tested on Oracle versions:
- Oracle Database 10g Enterprise Edition Release - 64bi - Oracle Database 11g Enterprise Edition Release - 64bit Production Received on Fri Oct 31 2008 - 05:21:12 CDT

Original text of this message