Default Namespace - targetNamespace or XMLSchema?

(A Collectively Developed Set of Schema Design Guidelines)

  XML Schemas: Best Practices     Hide (Localize) Versus Expose     Global versus Local     Element versus Type  
  Zero, One, or Many Namespaces     Variable Content Containers     Creating Extensible Content Models     Extending XML Schemas  

Table of Contents

Issue

When creating a schema should XMLSchema (i.e., http://www.w3.org/2001/XMLSchema) be the default namespace, or should the targetNamespace be the default, or should there be no default namespace?

Introduction

Except for no-namespace schemas, every XML Schema uses at least two namespaces - the targetNamespace and the XMLSchema (http://www.w3.org/2001/XMLSchema) namespace. There are three ways to design your schemas, with regards to dealing with these two namespaces:

1. Make XMLSchema the default namespace, and explicitly qualify all references to components in the targetNamespace.

2. Vice versa - make the targetNamespace the default namespace, and explicitly qualify all components from the XMLSchema namespace.

3. Do not use a default namespace - explicitly qualify references to components in the targetNamespace and explicitly qualify all components from the XMLSchema namespace.

Let's look at each approach in detail.

Approach 1: Default XMLSchema, Qualify targetNamespace

Below is a Library schema which demonstrates this design approach. It <include>s a Book schema, which contains a declaration for a Book element. The Library schema references ("ref"s) the Book element.
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.library.org"
        xmlns:lib="http://www.library.org"
        elementFormDefault="qualified">
    <include schemaLocation="Book.xsd"/>
    <element name="Library">
        <complexType>
             <sequence>
                 <element name="BookCatalogue">
                     <complexType>
                         <sequence>
                             <element ref="lib:Book"
                                      maxOccurs="unbounded"/>
                         </sequence>
                     </complexType>
                 </element>
            </sequence>
        </complexType>
    </element>
</schema>
Note that XMLSchema is the default namespace. Consequently, all the components used to construct the schema - element, include, complexType, sequence, schema, etc - have no namespace qualifier on them.

There is a namespace prefix, lib, which is associated with the targetNamespace. Any references (using the "ref" attribute) to components in the targetNamespace (Library, BookCatalogue, Book, etc) are explicitly qualified with lib (in this example there is a ref to lib:Book).

Advantages:

Clean design: there aren't many namespace qualifiers to clutter up the schema. So, the schema is quite readable.

Disadvantages:

For teaching purposes (or in doing a design review) we oftentimes want to focus on just a snippet of the schema. When examined outside the context of the entire schema, the namespace qualifier is often a source of confusion. For example, when this snippet is considered on its own:

<element ref="lib:Book" maxOccurs="unbounded"/>
It is the editor's experience that the namespace qualifier (lib) is frequently a source of confusion.

Approach 2: Qualify XMLSchema, Default targetNamespace

This design approach is the mirror image of the first approach:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.library.org"
            xmlns="http://www.library.org"
            elementFormDefault="qualified">
    <xsd:include schemaLocation="Book.xsd"/>
    <xsd:element name="Library">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="BookCatalogue">
                     <xsd:complexType>
                         <xsd:sequence>
                             <xsd:element ref="Book"
                                          maxOccurs="unbounded"/>
                         </xsd:sequence>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
With this approach all the components used to construct a schema (schema, element, complexType, sequence, etc) are namespace qualified (with xsd:).

There is a default namespace declaration that declares the targetNamespace to be the default namespace. Any references to components in the targetNamespace are not namespace qualified (note that the ref to Book is not namespace qualified).

Advantages:

For teaching purposes (or in doing a design review) we oftentimes want to focus on just a snippet of the schema. It is generally easier to understand a snippet when there is not a qualifier on references to targetNamespace components

Disadvantages:

Cluttered design: there are a lot of namespace qualifiers being used. So, the schema is a bit less readable.

Approach 3: No Default Namespace - Qualify both XMLSchema and targetNamespace

This design approach does not have a default namespace:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.library.org"
            xmlns:lib="http://www.library.org"
            elementFormDefault="qualified">
    <xsd:include schemaLocation="Book.xsd"/>
    <xsd:element name="Library">
        <xsd:complexType>
             <xsd:sequence>
                 <xsd:element name="BookCatalogue">
                     <xsd:complexType>
                         <xsd:sequence>
                             <xsd:element ref="lib:Book"
                                          maxOccurs="unbounded"/>
                         </xsd:sequence>
                     </xsd:complexType>
                 </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
Note that both the XMLSchema components are explicitly qualified, as well as references to components in the targetNamespace.

Advantages:

Very explicit: there are no implied (default) namespaces. Everything (all components and all references to components) is explicitly qualified.

Disadvantages:

Very cluttered: being very explicit by namespace qualifying everything can be annoying when reading the schema.

Best Practice

There is no clear-cut best approach with regards to this issue. In large part it is a matter of personal preference.

Editor's Note: At least when starting to learn XML Schemas it has been my experience that Approach 2 (Qualify XMLSchema, default targetNamespace) makes the schemas easiest to understand. The reason for this is:

- Qualifying the components that are used to construct a schema (schema, element, complexType, sequence, etc) is typically not a source of confusion since the qualifier is always the same (I always use xsd). That is, people get used to seeing xsd:schema, xsd:element, etc.

- On the other hand, the targetNamespaces may vary widely among schemas. Consequently, the namespace prefixes that one might generate can vary widely. For example, lib:Book, boston:subway. This variability is oftentimes a source of confusion.