Namespace-Related Facilities in XML Schema and in TSD4

This document discusses the following topics about namespace usage in XML Schema and TSD4:


The targetNamespace Attribute of the xs:schema Element

One major advantage of XML Schema compared to DTDs is that XML Schema fully supports XML Namespaces. In order to do so, XML Schema introduces the concept of the target namespace. Each schema document can declare at most one target namespace, and all definitions made in this schema document belong to this namespace. Thus, a schema may contain namespace-less definitions, or definitions belonging to the specified target namespace.

Does this really mean that we cannot define multi-namespace schemas? No; the emphasis is here on the schema document. We can always compose multi-namespace schemas by importing (see below) other schema documents into a schema, see the description of the xs:import element.

Schema Composition via xs:import

The xs:import statement is used in XML Schema to compose multi-namespace schemas. A typical example is given below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.softwareag.com/tamino/doc/examples/models/jazz/encyclopedia"
    xmlns="http://www.softwareag.com/tamino/doc/examples/models/jazz/encyclopedia"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:i="http://www.softwareag.com/tamino/doc/examples/models/instruments">
    
  <xs:import namespace="http://www.softwareag.com/tamino/doc/examples/models/instruments"
            schemaLocation="instruments.xsd"/>
  
...
</xs:schema>

xs:import statements are always given at the beginning of a schema definition. An xs:import statement usually specifies a namespace to import, and may optionally specify a URL using the schemaLocation attribute. Note that this attribute only gives a hint to the XML processor, telling it where to find the schema document associated with the imported namespace. XML processors are not required to use this attribute, but may use their own logic to find the associated schema. Tamino supports only relative URLs in the schemaLocation attribute in xs:import. The referenced schemas must already be defined in the Tamino database when defining the referencing schema.

import (with schemaLocation)

The structure of the schemaLocation attribute may be any of the following alternatives:

  • ../collectionName/schemaName

  • ./schemaName

  • schemaName

The last two alternatives are equivalent. Note: The xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes in XML instances of a schema (or doctype) are ignored by Tamino. Instead, a different logic based on collections and doctypes is applied to locate the schema used for validating the XML instance.

Example

The following schema shows the simultaneous use of the namespace and schemaLocation attributes of the <xs:import> element:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    targetNamespace="http://www.softwareag.com/tamino/doc/examples/models/jazz/encyclopedia"
    xmlns:e="http://www.softwareag.com/tamino/doc/examples/models/jazz/encyclopedia"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tsd="http://namespaces.softwareag.com/tamino/TaminoSchemaDefinition"
    xmlns:i="http://www.softwareag.com/tamino/doc/examples/models/instruments"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">
  <xs:import
     namespace="http://www.softwareag.com/tamino/doc/examples/models/instruments"
     schemaLocation="instruments.xsd"/>
  
  <xs:annotation>
    <xs:appinfo>
      <tsd:schemaInfo name="jazzMusician">
        <tsd:collection name="music"/>
        <tsd:doctype name="e:jazzMusician"/>
      </tsd:schemaInfo>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="jazzMusician">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="name" type="e:tName"/>
          ...
          <xs:element name="plays" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
              <xs:choice>
                <xs:element ref="i:saxophone"/>
              </xs:choice>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
        ...
      </xs:complexType>
    </xs:element>
    ...
</xs:schema>

This example schema defines a global element named jazzMusician, and local elements name and plays. The local elements are qualified because of the elementFormDefault="qualified" attribute. All the elements defined in the schema belong to the namespace given by the targetNamespace attribute.

The xs:import element imports a schema with definitions for the http://www.softwareag.com/tamino/doc/examples/models/instruments namespace. The content model of element plays refers to an element named saxophone in the imported namespace.

The imported schema might look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    targetNamespace = "http://www.softwareag.com/tamino/doc/examples/models/instruments"
    xmlns:i   = "http://www.softwareag.com/tamino/doc/examples/models/instruments"
    xmlns:xs  = "http://www.w3.org/2001/XMLSchema"
    xmlns:tsd = "http://namespaces.softwareag.com/tamino/TaminoSchemaDefinition">

  <xs:annotation>
    <xs:appinfo>
      <tsd:schemaInfo name="instruments.xsd">
        <tsd:collection name="music"/>
      </tsd:schemaInfo>
    </xs:appinfo>
  </xs:annotation>

  <xs:element name="saxophone">
    ...
  </xs:element>
  ...
</xs:schema>

Usage of the xs:import element is subject to various constraints with respect to:

  • the targetNamespace of the involved schema documents;

  • usage of the namespace attribute;

  • usage without the schemaLocation attribute.

Constraints (of xs:import statement)

The following constraints must be taken into account when using the xs:import element:

  • If there is no namespace attribute present, the enclosing xs:schema element must have a target namespace declaration.

  • If the xs:import element contains a namespace attribute, it may not match the value of the targetNamespace attribute of its enclosing xs:schema element.

Namespace of Elements and Attributes

Elements and attributes defined as child nodes of the xs:schema root element of a schema document are called global elements and attributes. If the xs:schema element has a targetNamespace attribute, all the global elements and attributes defined in that schema document are qualified, i.e. they belong to that namespace.

Once declared in this way, a global element or attribute can be used by referencing it in declarations (using the ref attribute of xs:element or xs:attribute). References to global elements or attributes are not restricted to a single schema; rather, they may refer to objects in a different namespace by using the prefix mapped to the targetNamespace of the imported schema in which the global object is defined.

By contrast, declarations of elements or attributes that are not immediate descendants of the xs:schema root node and do not just reference a global element or attribute definition via ref="..." are called local elements or local attributes. They can be defined e.g. inside a local or global (i.e. named) type. By default, these local nodes do not belong to the namespace given by the targetNamespace unless the corresponding element or attribute definition has a form="qualified" attribute. The default of form="unqualified" can be superseded by the elementFormDefault="qualified" or attributeFormDefault="qualified" attributes of the xs:schema element. These attributes specify whether the corresponding elements or attributes in the XML instance documents are qualified or not, if the form attribute is not present.

Global vs. Local Elements and Attributes

Namespace aspects of global and local elements and attributes in XML Schema are addressed in detail in the section Advanced Concepts I: Namespaces, Schemas & Qualification of the W3C XML Schema Part 0: Primer.

Wildcards

Another mechanism to allow for foreign namespaces is the usage of wildcards. A wildcard (i.e. an element or attribute whose content is not defined further) can be declared with the XML Schema elements xs:any or xs:anyAttribute. This allows for the inclusion of elements and attributes from foreign namespaces. For example, sections of XHTML, SVG, RDF or other content could be included into a document. A typical application is the description property in the style asset of our "Jazz" model, where we could use XHTML to mark up the content.

It is possible to constrain the namespace of the content of such a wildcard. This is done with the namespace attribute of the wildcard. This attribute can contain either:

  • a whitespace-separated list of the following items:

    • explicit namespace URIs;

    • the string "##targetNamespace", which denotes the target namespace of the current schema file;

    • the string "##local", which denotes the absence of a namespace;

  • or one of the following string values:

    • the string "##any", which allows for any namespace. This is also the default value of the namespace attribute. This value is often used together with processContents="skip";

    • the string "##other", which allows for any namespace other than the target namespace.

Qualified Type References

References to types must be qualified with the namespace prefix bound to the namespace in which the referenced type is defined.

Note:
This applies to both XML Schema predefined types and user-defined global types.

For example, the importing schema above contains a reference to a type e:tName, which might be defined as follows:

  <xs:complexType name="tName">
    <xs:sequence>
      <xs:element name="firstName" type="xs:string"/>
      <xs:element name="middleName" type="xs:string" minOccurs="0"/>
      <xs:element name="lastName" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

In the same way, references to a model group or attribute group definition in the same or a different schema document must use a prefix bound to a namespace that is identical to the respective schema's targetNamespace.