You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Webb Roberts <we...@gtri.gatech.edu> on 2003/06/21 00:48:34 UTC

Xerces validation / xsi:schemaLocation problem

I am running up against some strange behavior from Xerces-java 2.4.0.

Basically, I am specifying schema locations with an attribute in the
XML instance, and it is not resolving the locations of the schemas as
I would expect.  I have the following files:

./instance-fails.xml
./instance-validates.xml
./xsd/schema1.xsd
./xsd/schema2.xsd

Schema 1 references schema 2 via an import with no schemaLocation.
The instance that I would expect to validate follows
(instance-fails.xml):

<s1:document
     xmlns:s1='http://something.com/schema1'
     xmlns:s2='http://something.com/schema2'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://something.com/schema1 xsd/schema1.xsd
                         http://something.com/schema2 xsd/schema2.xsd'>
   <s2:document/>
</s1:document>

But, that one gives me the error:

WARNING in parsing
   file "file:///..../xsd/schema1.xsd", line 5
   src-import.0: Failed to read imported schema document 'xsd/schema2.xsd'.

However, if the instance is modified as follows
(instance-validates.xml):

<s1:document
     xmlns:s1='http://something.com/schema1'
     xmlns:s2='http://something.com/schema2'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://something.com/schema1 xsd/schema1.xsd
                         http://something.com/schema2 schema2.xsd'>
   <s2:document/>
</s1:document>

It validates.  It seems that the filename for schema2 has to be
relative to schema1, instead of relative to the XML instance.  The
only change is the filename for schema2.

Schema 1 is (xsd/schema1.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace='http://something.com/schema1'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'
             xmlns:s2='http://something.com/schema2'>
   <xsd:import namespace='http://something.com/schema2'/>
   <xsd:element name="document">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref='s2:document'/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>
</xsd:schema>

Schema 2 is (xsd/schema2.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace='http://something.com/schema2'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
   <xsd:element name="document"/>
</xsd:schema>

The validator I am using is available, as a source and compiled Jar,
from http://justicexml.gtri.gatech.edu/tools/index.html.  It is using
the EntityResolver from org.xml.sax.helpers.DefaultHandler.

Thanks,
Webb

--
Oh, this is the best pizza in a cup ever. This guy is unbelievable. He ran 
the old Cup 'o Pizza guy out of business. People come from all over to eat this.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Xerces validation / xsi:schemaLocation problem

Posted by Jeff Greif <jg...@alumni.princeton.edu>.
Just a guess, but I think the working model the spec-writers had in mind was
that if s1 imported s2 and s2 imported s3, that s1 would specify the
location of s2 (in the import element) and s2 would specify the location of
s3 (similarly).  In this case, the relative directory choices that actually
work make sense.  It also makes it easy to use s2 and s3 without s1.  That
is, you might specify an xsi:schemaLocation for s1, but allow the others to
be determined through the location attribute of the import elements, which
are interpreted relative to the current context in each case.

Jeff
----- Original Message ----- 
From: "Webb Roberts" <we...@gtri.gatech.edu>
To: <xe...@xml.apache.org>; <mr...@apache.org>
Sent: Monday, June 23, 2003 10:23 AM
Subject: Re: Xerces validation / xsi:schemaLocation problem


> It seems that relative URLs specified in xsi:schemaLocation fields
> must be specified relative to the schemas in which they are imported.
> I suppose that there is some logic to it, but I have to call exception
> to it being rather obscure and inconsistent.
>
> Follow this example: An instance needs to specify three schemas.  The
> first imports the second and the third, and the second imports the
> third.  The schemaLocation value that I would expect to work, which
> shows the directory structure relative to the xml file is:
>
>      xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
>                          http://something.com/schema2 s2d/d/schema2.xsd
>                          http://something.com/schema3 s3d/d/d/schema3.xsd'
>
> However, since schema1 is importing schema2 and schema3, the
> schemaLocation value should be relative to the location of schema1.xsd:
>
>      xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
>                          http://something.com/schema2 ../s2d/d/schema2.xsd
>                          http://something.com/schema3
../s3d/d/d/schema3.xsd'
>
> However, since schema 1 imports schema2 before it imports schema3, and
> schema2 imports schema3, the specification for schema3 must be
> relative to schema2, not to schema3, so the schemaLocation value must
> be:
>
>      xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
>                          http://something.com/schema2 ../s2d/d/schema2.xsd
>                          http://something.com/schema3
> ../../s3d/d/d/schema3.xsd'


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Xerces validation / xsi:schemaLocation problem

Posted by Webb Roberts <we...@gtri.gatech.edu>.
It seems that relative URLs specified in xsi:schemaLocation fields
must be specified relative to the schemas in which they are imported.
I suppose that there is some logic to it, but I have to call exception
to it being rather obscure and inconsistent.

Follow this example: An instance needs to specify three schemas.  The
first imports the second and the third, and the second imports the
third.  The schemaLocation value that I would expect to work, which
shows the directory structure relative to the xml file is:

     xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
                         http://something.com/schema2 s2d/d/schema2.xsd
                         http://something.com/schema3 s3d/d/d/schema3.xsd'

However, since schema1 is importing schema2 and schema3, the
schemaLocation value should be relative to the location of schema1.xsd:

     xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
                         http://something.com/schema2 ../s2d/d/schema2.xsd
                         http://something.com/schema3 ../s3d/d/d/schema3.xsd'

However, since schema 1 imports schema2 before it imports schema3, and
schema2 imports schema3, the specification for schema3 must be
relative to schema2, not to schema3, so the schemaLocation value must
be:

     xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
                         http://something.com/schema2 ../s2d/d/schema2.xsd
                         http://something.com/schema3 
../../s3d/d/d/schema3.xsd'

I think this level of complexity in the resolution of relative URIs is
too hard to specify.  In order to determine values which work, users
must trace through imports in schemas, and even then, any change in
import order in an imported schema may change the required definition
in the XML instance.

I have been unable to find language in the specification which
requires this behavior, although perhaps I don't know where to look.
Is what I'm seeing required by the specifications?  Which
specification and where?  Or was this a decision of the implementation
team?

Thanks,
Webb

the complete sample files are below:

<!-- ================ file s1d/schema1.xsd ================= -->

<?xml version="1.0"?>
<xsd:schema targetNamespace='http://something.com/schema1'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'
             xmlns:s2='http://something.com/schema2'
             xmlns:s3='http://something.com/schema3'>
   <xsd:import namespace='http://something.com/schema2'/>
   <xsd:import namespace='http://something.com/schema3'/>
   <xsd:element name="document">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref='s2:document'/>
         <xsd:element ref='s3:document'/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>
</xsd:schema>

<!-- ================ file s2d/d/schema2.xsd ================= -->

<?xml version="1.0"?>
<xsd:schema targetNamespace='http://something.com/schema2'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'
             xmlns:s3='http://something.com/schema3'>
   <xsd:import namespace='http://something.com/schema3'/>
   <xsd:element name="document">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref="s3:document"/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>
</xsd:schema>

<!-- ================ file s3d/d/d/schema3.xsd ================= -->

<?xml version="1.0"?>
<xsd:schema targetNamespace='http://something.com/schema3'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
   <xsd:element name="document"/>
</xsd:schema>

<!-- ================ file instance-fails-1.xml ================= -->

<?xml version="1.0"?>
<s1:document
     xmlns:s1='http://something.com/schema1'
     xmlns:s2='http://something.com/schema2'
     xmlns:s3='http://something.com/schema3'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
                         http://something.com/schema2 s2d/d/schema2.xsd
                         http://something.com/schema3 s3d/d/d/schema3.xsd'>
   <s2:document>
     <s3:document/>
   </s2:document>
   <s3:document/>
</s1:document>

<!-- ================ file instance-validates-1.xml ================= -->

<?xml version="1.0"?>
<s1:document
     xmlns:s1='http://something.com/schema1'
     xmlns:s2='http://something.com/schema2'
     xmlns:s3='http://something.com/schema3'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://something.com/schema1 s1d/schema1.xsd
                         http://something.com/schema2 ../s2d/d/schema2.xsd
                         http://something.com/schema3 
../../s3d/d/d/schema3.xsd'>
   <s2:document>
     <s3:document/>
   </s2:document>
   <s3:document/>
</s1:document>

<!-- ================ file s1d/schema1b.xsd ================= -->

<?xml version="1.0"?>
<xsd:schema targetNamespace='http://something.com/schema1'
             xmlns:xsd='http://www.w3.org/2001/XMLSchema'
             xmlns:s2='http://something.com/schema2'
             xmlns:s3='http://something.com/schema3'>
   <xsd:import namespace='http://something.com/schema3'/>
   <xsd:import namespace='http://something.com/schema2'/>
   <xsd:element name="document">
     <xsd:complexType>
       <xsd:sequence>
         <xsd:element ref='s2:document'/>
         <xsd:element ref='s3:document'/>
       </xsd:sequence>
     </xsd:complexType>
   </xsd:element>
</xsd:schema>

<!-- ================ file instance-validates-1b.xml ================= -->

<?xml version="1.0"?>
<s1:document
     xmlns:s1='http://something.com/schema1'
     xmlns:s2='http://something.com/schema2'
     xmlns:s3='http://something.com/schema3'
     xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
     xsi:schemaLocation='http://something.com/schema1 s1d/schema1b.xsd
                         http://something.com/schema2 ../s2d/d/schema2.xsd
                         http://something.com/schema3 ../s3d/d/d/schema3.xsd'>
   <s2:document>
     <s3:document/>
   </s2:document>
   <s3:document/>
</s1:document>



At 09:24 PM 6/20/2003, Michael Glavassevich wrote:
>As I understand it, relative URIs are relative to the resource in which
>they are referenced (that's not necessarily the resource in which they're
>declared), so if you're referencing schema 2 from schema 1, the URI for
>schema 2 is relative to schema 1, not your instance document.


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Xerces validation / xsi:schemaLocation problem

Posted by Michael Glavassevich <mr...@apache.org>.
Hi Webb,

As I understand it, relative URIs are relative to the resource in which
they are referenced (that's not necessarily the resource in which they're
declared), so if you're referencing schema 2 from schema 1, the URI for
schema 2 is relative to schema 1, not your instance document.

Also, DefaultHandler is just a convenience class. It's methods return
null, or are no-ops, so thus the entity resolver method of this class does
nothing. You have to extend DefaultHandler, and write an implementation
for resolveEntity if you want it to do more than that.

I hope that helps.

On Fri, 20 Jun 2003, Webb Roberts wrote:

> I am running up against some strange behavior from Xerces-java 2.4.0.
>
> Basically, I am specifying schema locations with an attribute in the
> XML instance, and it is not resolving the locations of the schemas as
> I would expect.  I have the following files:
>
> ./instance-fails.xml
> ./instance-validates.xml
> ./xsd/schema1.xsd
> ./xsd/schema2.xsd
>
> Schema 1 references schema 2 via an import with no schemaLocation.
> The instance that I would expect to validate follows
> (instance-fails.xml):
>
> <s1:document
>      xmlns:s1='http://something.com/schema1'
>      xmlns:s2='http://something.com/schema2'
>      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
>      xsi:schemaLocation='http://something.com/schema1 xsd/schema1.xsd
>                          http://something.com/schema2 xsd/schema2.xsd'>
>    <s2:document/>
> </s1:document>
>
> But, that one gives me the error:
>
> WARNING in parsing
>    file "file:///..../xsd/schema1.xsd", line 5
>    src-import.0: Failed to read imported schema document 'xsd/schema2.xsd'.
>
> However, if the instance is modified as follows
> (instance-validates.xml):
>
> <s1:document
>      xmlns:s1='http://something.com/schema1'
>      xmlns:s2='http://something.com/schema2'
>      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
>      xsi:schemaLocation='http://something.com/schema1 xsd/schema1.xsd
>                          http://something.com/schema2 schema2.xsd'>
>    <s2:document/>
> </s1:document>
>
> It validates.  It seems that the filename for schema2 has to be
> relative to schema1, instead of relative to the XML instance.  The
> only change is the filename for schema2.
>
> Schema 1 is (xsd/schema1.xsd):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema targetNamespace='http://something.com/schema1'
>              xmlns:xsd='http://www.w3.org/2001/XMLSchema'
>              xmlns:s2='http://something.com/schema2'>
>    <xsd:import namespace='http://something.com/schema2'/>
>    <xsd:element name="document">
>      <xsd:complexType>
>        <xsd:sequence>
>          <xsd:element ref='s2:document'/>
>        </xsd:sequence>
>      </xsd:complexType>
>    </xsd:element>
> </xsd:schema>
>
> Schema 2 is (xsd/schema2.xsd):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema targetNamespace='http://something.com/schema2'
>              xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
>    <xsd:element name="document"/>
> </xsd:schema>
>
> The validator I am using is available, as a source and compiled Jar,
> from http://justicexml.gtri.gatech.edu/tools/index.html.  It is using
> the EntityResolver from org.xml.sax.helpers.DefaultHandler.
>
> Thanks,
> Webb
>
> --
> Oh, this is the best pizza in a cup ever. This guy is unbelievable. He ran
> the old Cup 'o Pizza guy out of business. People come from all over to eat this.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>

--------------------
Michael Glavassevich
mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org