XML technologies / XML Schema / Controlling namespaces / References to other namespaces

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

One goal of the namespace specification is to allow documents mixing elements and attributes from different vocabularies. W3C XML Schema allows full use of this capability.

Part of the library vocabulary describes people and could be reused by other applications. We therefore want a separate namespace with URI reference "http://dyomedea.com/ns/people":

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

We need two schemas, one per namespace. One describes the people vocabulary and defines element person and its children:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/people" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:ppl="http://dyomedea.com/ns/people" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <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:schema>

This schema describes namespace "http://dyomedea.com/ns/people", without including anything from other namespaces. It resembles our previous examples. It works independently for documents using only this namespace, but can also be imported by schemas describing others that want to reuse some definitions.

The including vocabulary’s schema needs two pieces of information. It needs a prefix for the included namespaces, supplied by an ordinary namespace declaration, and a hint locating their schema, supplied through element xs:import:

<xs:import namespace="http://dyomedea.com/ns/people" schemaLocation="simple-2-ns-ppl.xsd"/>

The schema now has all information needed to resolve references to components in namespace "http://dyomedea.com/ns/people". References can simply use its prefix:

<xs:element ref="ppl:person"/>

A complete library-vocabulary schema could then be:

<?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:ppl="http://dyomedea.com/ns/people" xmlns:lib="http://dyomedea.com/ns/library">
    <xs:import namespace="http://dyomedea.com/ns/people" schemaLocation="simple-2-ns-ppl.xsd"/>
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="lib:bookType"/>
            </xs:sequence>
        </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="ppl:person"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="characters">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element ref="ppl: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>

When importing schemas from other namespaces, only their global components can be referenced.

   

 

 

 

back to top