XML technologies / XML Schema / Controlling namespaces / To qualify or not to qualify—that is the question

(Excerpt from “XML Schema” by Eric van der Vlist)

The schemas we have written so far had no target namespace declaration. We could also describe only elements and attributes belonging to no namespace.

Declaring a target namespace allows us to define elements and attributes belonging to it, called “qualified”, as well as those belonging to no namespace, called “unqualified”.

A schema’s purpose is to describe a vocabulary. Its top-level nodes belong to its target namespace. Therefore, when a target namespace is declared, defining unqualified global elements is forbidden.

Qualified and unqualified elements and attributes are distinguished by their attribute form. For example:

<xs:element name="book" form="qualified"/>

<xs:attribute name="isbn" form="qualified"/>

<xs:attribute name="lang" form="unqualified"/>

<xs:element name="character" form="unqualified"/>

The default values of these form attributes are set by the attributes elementFormDefault and attributeFormDefault that we added to our element xs:schema.

These two attributes in turn have default values of elementFormDefault="unqualified" and attributeFormDefault="unqualified". These values are appropriate when only the document element uses a namespace:

<lib:library xmlns:lib="http://dyomedea.com/ns/library">
  <book id="b3810518883" available="yes">
     <isbn>
       3810518883
     </isbn>
     <title>
       Auf den Hund gekommen
     </title>
  </book>
</lib:library>

Since global elements and attributes must be qualified, defining this schema as a coherent whole requires all elements and attributes to be defined locally.

Another combination, elementFormDefault="qualified" and attributeFormDefault="unqualified", fits the common case where a namespace is associated with the root element and thus becomes the default namespace, which by definition applies to its descendant elements but not its attributes. According to the namespace Recommendation, the default namespace does not apply to attributes.

<library xmlns="http://dyomedea.com/ns/library">
    <book id="b3810518883" available="yes">
        <isbn>
            3810518883
        </isbn>
        <title>
            Auf den Hund gekommen
        </title>
    </book>
</library>

By convention, unqualified attributes are considered part of the same vocabulary as their parent element. Vocabularies using qualified attributes often provide attributes for use in elements from other namespaces. Examples include RDF and attribute-only vocabularies such as XLink and XML Base. The schemas for these vocabularies benefit from setting attributeFormDefault to “qualified”.

The special status of attributes in the namespace specification deserves particular attention. The default namespace does not apply to attributes. An attribute without a prefix is therefore considered to have no namespace.

Another confusing aspect of prefixed attributes, which therefore belong to a namespace, is that the namespace Recommendation calls them “global attributes”. In W3C XML Schema, “global” has a different meaning, in contrast to local elements or attributes. A global attribute in the terminology of Namespaces is thus a qualified attribute in the terminology of W3C XML Schema, and may be defined globally or locally.

Remember that <elementFormDefault> and <attributeFormDefault> set default values, and that qualification can be specified separately for each element and attribute.

Before examining how to introduce other namespaces, consider a simple instance document with a single namespace and unqualified attributes:

<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library">
    <book id="b3810518883" available="yes">
        <isbn>
            3810518883
        </isbn>
        <title>
            Auf den Hund gekommen
        </title>
        <authors>
            <person id="CMS">
                <name>
                    Charles M. Schulz
                </name>
                <born>
                    1922-11-26
                </born>
                <dead>
                    2000-02-12
                </dead>
            </person>
        </authors>
        <characters>
            <person id="PP">
                <name>
                    Peppermint Patty
                </name>
                <born>
                    1966-08-22
                </born>
                <qualification>
                    kühn, dreist und draufgängerisch
                </qualification>
            </person>
            <person id="Snoopy">
                <name>
                    Snoopy
                </name>
                <born>
                    1950-10-04
                </born>
                <qualification>
                    extrovertierter Beagle
                </qualification>
            </person>
            <person id="Schroeder">
                <name>
                    Schroeder
                </name>
                <born>
                    1951-05-30
                </born>
                <qualification>
                    brachte die klassische Musik in die Peanuts-Comics ein
                </qualification>
            </person>
            <person id="Lucy">
                <name>
                    Lucy
                </name>
                <born>
                    1952-03-03
                </born>
                <qualification>
                    herrschsüchtig, kratzbürstig und egoistisch
                </qualification>
            </person>
        </characters>
    </book>
</library>

To avoid confusion when writing a schema for this instance document, we can define prefixes in the schema both for our target namespace and for the namespace of W3C XML Schema, producing a schema like this:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="lib:bookType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="born" type="xs:date"/>
                <xs:element name="dead" type="xs:date" minOccurs="0"/>
                <xs:element name="qualification" type="xs:string" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="isbn" type="xs:NMTOKEN"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="authors">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="lib:person" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="characters">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="lib:person" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="required"/>
        <xs:attribute name="available" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

In this example, names of components defined by the schema always have no prefix at their definition. Because this schema defines components only in the single namespace "http://dyomedea.com/ns/library", specified by the attribute targetNamespace of the element xs:schema, there is no risk of confusion. The only other namespaces appearing here are the namespace for W3C XML Schema itself, identified by the prefix xs, and another mapping for the namespace "http://dyomedea.com/ns/library", which uses lib as its prefix. The form with the prefix lib is used for cross-references between declarations.

A strictly equivalent schema defining exactly the same data model can be written using the target namespace as the schema document’s default namespace:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://dyomedea.com/ns/library">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="bookType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="born" type="xs:date"/>
                <xs:element name="dead" type="xs:date" minOccurs="0"/>
                <xs:element name="qualification" type="xs:string" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="isbn" type="xs:NMTOKEN"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="authors">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="person" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="characters">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="person" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="required"/>
        <xs:attribute name="available" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

In this version, all references to the elements belonging to W3C XML Schema still use the prefix xs . Component names remain unprefixed, but the prefix lib is now unnecessary, so all such prefixes can disappear. Since the default namespace is defined in this document, W3C XML Schema understands the connection between the components defined there and references to them.

If you prefer to use prefixes for the components you define and use the vocabulary of W3C XML Schema without prefixes, you can also make the W3C XML namespace the default namespace and use a prefixed namespace, here lib, for the components you define:

<?xml version="1.0"?>
<schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:lib="http://dyomedea.com/ns/library">
    <element name="library">
        <complexType>
            <sequence>
                <element name="book" type="lib:bookType"/>
            </sequence>
        </complexType>
    </element>
    <element name="person">
        <complexType>
            <sequence>
                <element name="name" type="string"/>
                <element name="born" type="date"/>
                <element name="dead" type="date" minOccurs="0"/>
                <element name="qualification" type="string" minOccurs="0"/>
            </sequence>
            <attribute name="id" type="ID" use="required"/>
        </complexType>
    </element>
    <complexType name="bookType">
        <sequence>
            <element name="isbn" type="NMTOKEN"/>
            <element name="title" type="string"/>
            <element name="authors">
                <complexType>
                    <sequence>
                        <element ref="lib:person" maxOccurs="unbounded"/>
                    </sequence>
                </complexType>
            </element>
            <element name="characters">
                <complexType>
                    <sequence>
                        <element ref="lib:person" maxOccurs="unbounded"/>
                    </sequence>
                </complexType>
            </element>
        </sequence>
        <attribute name="id" type="ID" use="required"/>
        <attribute name="available" type="string" use="required"/>
    </complexType>
</schema>

References to the data types of W3C XML Schema now have no prefix, whereas references to components defined in the target namespace use the prefix lib.

   

 

 

 

back to top