xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anthony Dodd <AnthonyD...@synergy-fs.com>
Subject RE: Problems finding schema definition
Date Thu, 07 Sep 2000 07:44:18 GMT
Hi Andy

	Very well explained, I've got a question that you might be able to
answer. I:ve got a series of xml configuration files for
my application. These files are held in a jar file. I wanted to place the
schemas for these xml files in the same jar file. I was
wondering if you could tell me how I code the xsi:schemaLocation attribute
to tell xerces to pick up the grammar from with in a jar file. 

	The schemas will be in the same package as the xml configuration
files. I tried xsi:schemaLocation="http://www.mycompany.com /myschema.xsd"
but it refused to recognise it as being relative to the xml file being
validated. After looking at the code in XMLValidator.java and
DefaultEntityHandler.java, I realised that the above xsi:schemaLocation
attribute was directing xerces to look for schemas in user.dir. Is there any
way I can get this to work. Thanks in advance for any help offered.

Regards
Anthony Dodd
  
-----Original Message-----
From: Andy Clark [mailto:andyc@apache.org]
Sent: 06 September 2000 18:39
To: general@xml.apache.org
Subject: Re: Problems finding schema definition


Andy_Carr@tertio.com wrote:
>     <GMI xmlns="file:///H:/Andy/Programs/Web/XML/GMI.xsd">
> [...]
>     <schema xmlns="http://www.w3.org/1999/XMLSchema">

Okay, there are two problems with your XML and XSD files.

1) Your XML file is in a namespace but your XML Schema grammar
doesn't specify a target namespace. If your document instance
is in a namespace, then your grammar has to specify a target
namespace and the two must match! Here's an example of a 
grammar:

  <!-- grammar.xsd -->
  <schema xmlns='http://www.w3.org/1999/XMLSchema'
          targetNamespace='NS'>
   <element name='root'>
    <complexType>
     <element ref='sub'/> <!-- WRONG -->
    </complexType>
   </element>
   <element name='sub'/>
  </schema>

However, this leads to a common problem. When you specify a
target namespace in your XML Schema document, then all of the
references to element types and attribute groups are QNames!
What does this mean? This means that my example is wrong.

The Schema processor will look at "sub" as a qualified name
so it thinks that you are referring to an element declared
in the default namespace. And since I have bound the default
namespace to "http://www.w3.org/1999/XMLSchema", it will look
in *that* grammar for an element called "sub".

So how do we avoid this problem? Well, we have to fully
qualify the "sub" element reference which, in turn, means
that we need to bind that namespace. Here's the fix:

  <!-- grammar.xsd -->
  <schema xmlns='http://www.w3.org/1999/XMLSchema'
          xmlns:a='NS' targetNamespace='NS'>
   <element name='root'>
    <complexType>
     <element ref='a:sub'/> <!-- RIGHT -->
    </complexType>
   </element>
   <element name='sub'/>
  </schema>

Notice that the prefix "a" is bound to *exactly* the same
namespace string as is specified in the targetNamespace
attribute!

Now on to your second problem...

2) The namespace binding is not the way in which that you 
tell the XML Schema processor to find your Schema grammar. The
namespace is only a means of uniquely qualifying a namespace.
So the following is incorrect.

  <!-- document.xml -->
  <root xmlns='NS'> <!-- WRONG -->
   <sub/>
  </root>

First you have to bind the XML Schema instance document
namespace and then use the special attribute "schemaLocation"
in order to locate your grammar. (The specification says
that this is only a "hint" but who are they kidding...) The
value of schemaLocation is a set of tuples: each tuple is a
pair of namespace and grammar location. For example:

  <!-- document.xml -->
  <root xmlns='NS' 
        xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance'
        xsi:schemaLocation='NS grammar.xsd'>
   <sub/>
  </root>

Also, you probably shouldn't use 'file:///.../GMI.xsd' as 
your namespace binding; it's really the location of your 
grammar and should only be used in the xsi:schemaLocation 
or xsi:noNamespaceSchemaLocation attribute.

Some disclaimers:

a) Schema is confusing so if I've made any mistakes in the
   above examples, someone please correct me.
b) I really shouldn't be using "NS" as my namespace binding.
   I'm only using it to simplify my example; otherwise
   *everything* is a URI and confuses the person trying to
   figure out what's related to what.

-- 
Andy Clark * IBM, JTC - Silicon Valley * andyc@apache.org

---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org

Mime
View raw message