8
votes

I would like to ask a question about XML document validation against its corresponding XML schema(s) and I would appreciate if you could kindly give me a hand. Actually, I've just started learning about XML schemas (I'm totally a beginner). I've purchased the book "Definitive XML Schema" written by Priscilla Walmsley (2nd Edition) which presents XML Schema 1.1 (which I believe is the most recent version).

Now the problem is that in all examples and exercices of the book, the namespaces and location of the schema files are given using a web URL.

Here is an example of the book:

This is the schema

<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 to be validated against the above mentioned is this

<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 DELETE any file on this site for my exercices while I'm reading this book. As a result I need to put both XML and XSD documents on my local hard drive (I'm using Linux Fedora Core 17 X86_64). So what I did, was to put the content of the schema in a file named for example 'Example-01.xsd' and the XML content in a file named 'Example-01.xml'.

I use oracle PL/SQL package DBMS_XMLSCHEMA (Enterprise Edition 11.2.0.1.0) in order to first register the schema and then call the validate method of XMLType object in order to validate my XML document against the schema, similar to the following link:

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

I have created a directory (by CREATE DIRECTORY) statement in oracle :

/home/train/Documents/myutl_file_dir/

Where I put both my XML & XSD documents. Here is precisely how I changed the above mentioned XML content in order to refer 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 compare the new XML content with the above mentioned example, the only thing that I added was file:///home/train/Documents/myutl_file_dir/Example-01.xsd at the end of the value of xsi:schemaLocation in order 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 doesn't work. When I run my script in order to validate the document by calling the schemaValidate() method of the XMLType object, here is 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 'Method invalid for non-schema based XML Documents' is that oracle has simply ignored the filepath that I defined for schema file and it considers that there has not been any schema declared for this XML document.

Any idea? How can I deal with this problem?

Thanks in advance,

Dariyoosh

2

2 Answers

8
votes

OK, after a lot of Googling and thanks to stefan nebesnak' comment I found two problems that caused the error:

The first one, as Stefan mentioned is that DBMS_XMLSCHEMA.registerSchema can be applied only to schema-based document. Initially what I had done 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 absolutely no link between the registered schema and the XMLType object that refers to my XML document. What I had to do was 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 solved and my XML document became a schema-based.

The second problem was the fact that in the book the XML document to be validated against the schema had been defined in following way:

<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 of it, that is, http://datypic.com/prod refers to the targetNameSpace and the second part separated by a space, that is, prod.xsd, is used to refer to the location (as an URL) of the schema document which is true. But what I didn't know, was that in oracle DBMS_XMLSCHEMA implementation, this second part must be the very same URL used to register the schema by calling DBMS_XMLSCHEMA.RegisterSchema method, otherwise it will not work. And here is the 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 a 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:

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

And that was precisely what I hadn't done, I had registered the schema with 'http://datypic.com/prod' and just because in the book there was a file name as the second part of the xsi:schemaLocation (prod.xsd) I thought that all I had to do was to put the schema content in a file named for example Example-01.xsd and then provide the file URL to this document, file:///home/train/Documents/myutl_file_dir/Example-01.xsd. Which was really a silly thing because, when you register a schema with oracle by calling the DBMS_XMLSchema.registerSchema, oracle registers the content of the XML schema document. So what oracle needs is the schemaURL used during registeration which allows to find and identify uniquely the specific schema. And this was exactly my mistake I had thought that oracle is looking for physical location of my file.

Conclusion: I had registered the schema by the targetNameSpace : 'http://datypic.com/prod' (well, it was just an example, I could have chosen other value)

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

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

And this solved the problem.

I hope this can help those who have encountered the same problem.

Regards,

Dariyoosh

0
votes

The method can be invoked on only schema based xmltype objects.

Try delete and reload documents, before registering an XML schema they reference.

Oracle XML DB Developer's Guide:

When you register an XML schema, keep in mind that the act of registering a schema has no effect on the status of any instance documents already loaded into Oracle XML DB Repository that reference the XML schema. Because the XML schema was not yet registered, such instance documents were non-schema-based when they were loaded. They remain non-schema-based after the schema is registered.

You must delete such instance documents, and reload them after registering the schema, in order to obtain schema-based documents.

Oracle XML Storage Tutorial.

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

    xsi:noNamespaceSchemaLocation="Example-01.xsd"

(valid relative URI reference)