Discussions
Categories
- 17.9K All Categories
- 3.4K Industry Applications
- 3.3K Intelligent Advisor
- 62 Insurance
- 536K On-Premises Infrastructure
- 138.2K Analytics Software
- 38.6K Application Development Software
- 5.7K Cloud Platform
- 109.4K Database Software
- 17.5K Enterprise Manager
- 8.8K Hardware
- 71.1K Infrastructure Software
- 105.2K Integration
- 41.6K Security Software
XML validation with xsd doesn't work with dates

Oracle version 19.11
Simple xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="test" type="ISODate"/> <xs:simpleType name="ISODate"> <xs:restriction base="xs:date"> <xs:pattern value="[0-9]{4,4}\-[0-9]{2,2}\-[0-9]{2,2}"/> </xs:restriction> </xs:simpleType> </xs:schema>
When xml is
<test>10-JAN-10</test>
schemavalidate doesn't raise an error that it's invalid value.
Full example:
--exec dbms_xmlschema.deleteschema('test.xsd', 3); declare l_content clob:= '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="test" type="ISODate"/> <xs:simpleType name="ISODate"> <xs:restriction base="xs:date"> <xs:pattern value="[0-9]{4,4}\-[0-9]{2,2}\-[0-9]{2,2}"/> </xs:restriction> </xs:simpleType> </xs:schema> '; begin dbms_xmlschema.registerschema( schemaurl => 'test.xsd' , schemadoc => l_content , local => true , gentypes => false , gentables => false , genbean => false , force => false ); end; / declare l_test xmltype :=xmltype('<test>10-JAN-10</test>'); l_xml xmltype; begin l_xml:=l_test.createSchemaBasedXML('test.xsd'); l_xml.schemaValidate(); end; /
However using xmlint
<test>10-JAN-10</test> test.xml:1: element test: Schemas validity error : Element 'test': '10-JAN-10' is not a valid value of the atomic type 'ISODate'. test.xml fails to validate
What is missing to validate the xml correctly?
Best Answer
-
You have to register for Binary XML for the pattern facet validation to work.
Don't ask me why. The regular registration seems to ignore those additional restrictions and use some SQL date validation internally, which is also dependent on NLS settings it would appear.
dbms_xmlschema.registerschema( ... , options => dbms_xmlschema.REGISTER_BINARYXML );
then:
declare l_test xmltype :=xmltype('<test>10-JAN-10</test>'); l_xml xmltype; begin l_xml:=l_test.createSchemaBasedXML('test.xsd'); l_xml.schemaValidate(); end; / ORA-31154: invalid XML document ORA-19202: Error occurred in XML processing LSX-00249: invalid date or time "10-JAN-10" ORA-06512: at "SYS.XMLTYPE", line 354 ORA-06512: at line 7
Answers
-
From what I recall and could verify via a very old post
you need to add xsi:noNamespaceSchemaLocation="test.xsd" to your root node.
-
Hi,
also doesn't work. Still doesn't raise an error that date is invalid.
declare l_test xmltype :=xmltype('<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">10-JAN-10</test>'); l_xml xmltype; begin l_xml:=l_test.createSchemaBasedXML('test.xsd'); l_xml.schemaValidate(); end; /
xsi is probably also not needed when specifying it already createSchemaBasedXML('test.xsd')
-
You have to register for Binary XML for the pattern facet validation to work.
Don't ask me why. The regular registration seems to ignore those additional restrictions and use some SQL date validation internally, which is also dependent on NLS settings it would appear.
dbms_xmlschema.registerschema( ... , options => dbms_xmlschema.REGISTER_BINARYXML );
then:
declare l_test xmltype :=xmltype('<test>10-JAN-10</test>'); l_xml xmltype; begin l_xml:=l_test.createSchemaBasedXML('test.xsd'); l_xml.schemaValidate(); end; / ORA-31154: invalid XML document ORA-19202: Error occurred in XML processing LSX-00249: invalid date or time "10-JAN-10" ORA-06512: at "SYS.XMLTYPE", line 354 ORA-06512: at line 7
-
you need to add xsi:noNamespaceSchemaLocation="test.xsd" to your root node.
You would need that in order to call createSchemaBasedXML() without any argument.
-
options => dbms_xmlschema.REGISTER_BINARYXML
That solved by problem. Thanks!