LC-98. ns-and-schemaloc: Clarifying schema location and namespace

"gmacri@libero.it"<gmacri@libero.it> to XML Schema Comments list, Sat, 6 May
2000 14:20:01 +0200:
>
>  In this example there are three declarations of namespace and two declaration
of schemalocation that are independent. 
>
>  <Library 
>       xmlns:book  ="http://www.somewhere.org/Book" 
>       xmlns:person="http://www.somewhere-else.org/Person" 
>       xmlns:xsi   ="http://www.w3.org/1999/XMLSchema/instance" 
>       xsi:schemaLocation="http://www.somewhere.org/Examples 
>         http://www.somewhere.org/Examples/Book.xsd 
>         http://www.somewhere-else.org/Person 
>         http://www.somewhere-else.org/Person/Person.xsd">
>     <BookCatalogue>
>       <book:Book>
>            <book:Title>Illusions The Adventures of a Reluctant 
>              Messiah</book:Title> 
>            <book:Author>Richard Bach</book:Author> 
>            <book:Date>1977</book:Date> 
>            <book:ISBN>0-440-34319-4</book:ISBN> 
>            <book:Publisher>Dell Publishing Co.</book:Publisher> 
>        </book:Book>
>
>  When I analyze this document with an application (automatically), how I can
understand that, for example, 
>  for "Book" element I must search it in the schema "Book.xsd" and not in
"Person.xsd", to verify the validity of this document? 

Thanks for the question.  Associating a schema to a namespace while avoiding
conflict with the stated intent of the Namespace specification does take a
couple of steps, but it's really not too complex.

The example above is only slightly wrong:

>  <Library        xsi:schemaLocation="http://www.somewhere.org/Examples 
>         http://www.somewhere.org/Examples/Book.xsd 
>         http://www.somewhere-else.org/Person 
>         http://www.somewhere-else.org/Person/Person.xsd">

should be:

   <Library        xsi:schemaLocation="http://www.somewhere.org/Book
          http://www.somewhere.org/Examples/Book.xsd 
          http://www.somewhere-else.org/Person 
          http://www.somewhere-else.org/Person/Person.xsd">

Note that the first entry in the schemaLocation attribute has been changed to
"Book" from "Examples".  The entries in the "xsi:schemaLocation" attribute in
the instance document are namespace/location pairs; in the case of the namespace
"http://www.somewhere.org/Book" the schema location hint is
"http://www.somewhere.org/Examples/Book.xsd".

Further, the schema itself will name a target namespace, which must match the
namespace described in the instance document, for example:

   <xsd:schema xsd:targetNamespace="http://www.somewhere.org/Book" 
         xmlns:local="http://www.somewhere.org/Book" 
         xmlns:xsd="http://www.w3.org/1999/XMLSchema" >

         <element name="Book" xsd:type="local:BookType" />

         <complexType name="BookType">
              <element name="Title" xsd:type="string" />
               ...
         </complexType>
    </xsd:schema>

Whether schemas are loaded from the "schemaLocation" hints is at processor
discretion; the designed {targetNamespace} for the schema must simply match one
of the referenced namespaces (prefixed or not) in the document for its
components to be used.  As Henry Thompson noted earlier, the processor may opt
not to load any schema:  it may already know what schema to use for a designated
namespace.

While this two step process of element bound to namespace followed by namespace
bound to schema location may seem cumbersome, it appropriately and effectively
decouples namespace definitions from the locations of schemas which fill them
with names.

One additional point:  to provide a location hint for a schema document with the
"no namespace" elements in an instance document (i.e. no prefixes), use the
"xsi:noNamespaceSchemaLocation" attribute instead of the "xsi:schemaLocation"
attribute.  A slight modification of your intial example follows:

  <Library 
       xmlns:book  ="http://www.somewhere.org/Book" 
       xmlns:person="http://www.somewhere-else.org/Person" 
       xmlns:xsi   ="http://www.w3.org/1999/XMLSchema/instance" 
       xsi:schemaLocation="http://www.somewhere-else.org/Person 
         http://www.somewhere-else.org/Person/Person.xsd"
	
xsi:noNamespaceSchemaLocation="http://www.somewhere.org/Examples/Book.xsd">
     <BookCatalogue>
       <Book>
            <Title>Illusions The Adventures of a Reluctant 
              Messiah</Title> 
            <Author>Richard Bach</Author> 
            <Date>1977</Date> 
            <ISBN>0-440-34319-4</ISBN> 
            <Publisher>Dell Publishing Co.</Publisher> 
        </Book>
 
Of course, "BookCatalog" would also be expected to be in the schema now, but
we've given the processor a hint at where to find elements and attributes which
appear to have no namespace (i.e. prefix).

Thanks for taking the time to comment.  I hope this explanation helps.  

-David Ezell
for the W3C XML Schema Working Group
        

Received on Friday, 9 June 2000 09:34:39 UTC