xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Clark <an...@apache.org>
Subject Re: Problems finding schema definition
Date Wed, 06 Sep 2000 17:38:50 GMT
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

Mime
View raw message