Can Oracle validate XML using the XSD schema on the local file system?

I would like to ask a question about validating an XML document regarding its corresponding XML schemas (schemas), and I would appreciate it if you could kindly give me a hand. In fact, I just started to learn XML schemas (I'm completely new). I bought the Definitive XML Schema book, written by Priscilla Walmsley (2nd edition), which introduces the XML Schema 1.1 (which I believe is the latest version).

Now the problem is that in all the examples and exercises, the namespace books and the location of the schema files are given using a web URL.

Here is an example book:

This is a circuit

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/prod" xmlns:prod="http://datypic.com/prod"> <xs:element name="product" type="prod:ProductType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="size" type="prod:SizeType"/> </xs:sequence> <xs:attribute name="effDate" type="xs:date"/> </xs:complexType> <xs:simpleType name="SizeType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="2"/> <xs:maxInclusive value="18"/> </xs:restriction> </xs:simpleType> </xs:schema> 

And the XML content that needs to be validated above is

 <prod:product xmlns:prod="http://datypic.com/prod" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datypic.com/prod prod.xsd" effDate="2011-04-12"> <number>557</number> <size>10</size> </prod:product> 

Obviously, [http://datypic.com/prod] is a site maintained by the author, so I cannot add or remove any file on this site for my exercises while I am reading this book. As a result, I need to place both XML and XSD documents on my local hard drive (I use Linux Fedora Core 17 X86_64). So what I did was to put the contents of the schema in a file called " Example-01.xsd " and the XML content in a file called " Example-01.xml .

I use the oracle PL / SQL package DBMS_XMLSCHEMA (Enterprise Edition 11.2.0.1.0) to register the schema first and then call the XMLType object validation method to check my XML document for a schema similar to the following link:

https://forums.oracle.com/forums/thread.jspa?messageID=2462207

I created a directory (via CREATE DIRECTORY) in oracle:

/ home / train / Documents / myutl_file_dir /

Where I put both of my XML and XSD documents. Here's how I modified the above XML content to link to XSD locally

 <prod:product xmlns:prod="http://datypic.com/prod" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://datypic.com/prod file:///home/train/Documents/myutl_file_dir/Example-01.xsd" effDate="2011-04-12"> <number>557</number> <size>10</size> </prod:product> 

So, if you are comparing the new XML content with the above example, the only thing I added is file: ///home/train/Documents/myutl_file_dir/Example-01.xsd at the end of the xsi: schemaLocation value to refer to the local hard drive. I found this method here:

http://lists.w3.org/Archives/Public/xmlschema-dev/2001Dec/0161.html

Now the problem is that it does not work. When I run my script to check the document by calling the schemaValidate () method of the XMLType object, here is the oracle error message:

 BEGIN * ERROR at line 1: ORA-19030: Method invalid for non-schema based XML Documents. ORA-06512: at "SYS.XMLTYPE", line 354 ORA-06512: at "TRAIN2012.ZXML_PKG", line 176 ORA-06512: at line 2 

What I understand from the error message " Invalid method for schema-based XML documents, is that oracle simply ignored the path to the file that I defined for the schema file, and considers that there was not any schema. declared for this XML document.

Any idea? How can I solve this problem?

Thanks in advance,

Dariyoosh

+7
source share
2 answers

Ok, after a lot of google and thanks to the comments of stefan nebesnak, I found two problems that caused the error:

The first, as Stefan noted, is that DBMS_XMLSCHEMA.registerSchema can only be applied to a schema-based document. Initially, what I did in my PL / SQL code was something like this:

 l_xml_file_content := XMLType('Here I put the content of my XML document') l_xml_file_content.schemaValidate(); 

So, as you can see, there is no connection between the registered schema and the XMLType object that references my XML document. I had to do this:

 l_xml_file_content := XMLType( 'Here I put the content of my XML document' ).createSchemaBasedXML('and here I put the very same schema URL used during registereation while I registered the schema by calling the DBMS_XMLSCHEMA.registerSchema method') 

So, the first problem was resolved, and my XML document became schema based.

The second problem was that in the book, the XML document to be checked against the schema was defined as follows:

 <prod:product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:prod="http://datypic.com/prod" xsi:schemaLocation="http://datypic.com/prod prod.xsd" effDate="2011-04-12"> <number>557</number> <size>10</size> </prod:product> 

What I understood from " xsi: schemaLocation =" http://datypic.com/prod prod.xsd " was that the first part, that is, http://datypic.com/prod relates to targetNameSpace, and the second part, separated by a space, i.e. prod.xsd , is used to refer to the location (as the URL) of the schema document, which is true, but what I did not know was that in the Oracle DBMS_XMLSCHEMA implementation this second part should be by the same URL that is used to register the schema by calling the DBMS_XMLSCHEMA.RegisterSchema method, otherwise it will not work. And here is a link to the oracle documentation that confirms this:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb03usg.htm#BABBGBDA

If the target XML schema declares the target namespace, then the schemaLocation attribute is used to identify the XML schema. The value of this attribute is a pair of values ​​separated by a space:

  • value of the target namespace declared in the XML schema
  • schema location hint, unique identifier passed to the DBMS_XMLSCHEMA.registerSchema procedure when the schema is registered in the database

And this is exactly what I did not, I registered the scheme with http://datypic.com/prod , and only because the book had the file name as the second part of xsi: schemaLocation (prod.xsd) I thought that was all what I needed to do was place the contents of the schema in a file named example-01.xsd and then specify the URL of this document, file: ///home/train/Documents/myutl_file_dir/Example-01.xsd . This was really stupid, because when you register a schema with oracle by calling DBMS_XMLSchema.registerSchema, oracle registers the contents of the XML schema document. Thus, what the oracle needs is the schemaURL used during registration, which allows you to uniquely identify and identify a particular scheme. And that was my mistake, I thought the oracle was looking for the physical location of my file.

Conclusion: I registered the scheme with targetNameSpace: 'http://datypic.com/prod' (well, that was just an example, I could choose a different value)

So, the correct schemaLocation value inside my XML document was like this:

 xsi:schemaLocation="http://datypic.com/prod prod.xsd http://datypic.com/prod prod.xsd" 

And that solved the problem.

Hope this helps those facing the same problem.

Hi,

Dariyoosh

+7
source

This method can only be called on xmltype-based objects .

Try deleting and reloading documents before registering the XML schema that they reference.

Oracle XML DB Developer's Guide :

When you register an XML schema, keep in mind that the action of registering a schema does not affect the status of any instance of documents already loaded into the Oracle XML DB repository that reference XML schemas. Since the XML schema has not yet been registered, such if they were loaded, the instance documents were non-competitive. They remain without a scheme after registering the scheme.

You must delete such instance documents and reload them after registering the scheme to obtain documents based on the scheme.

Oracle XML Storage Tutorial .

You can place the schema in the same directory as the XML that references it. In this case, you need to specify only the file name:

  xsi:noNamespaceSchemaLocation="Example-01.xsd" 

(valid relative URI reference)

0
source

All Articles