Book HomeXML SchemaSearch this book

10.6. Referencing Other Namespaces

One of the goals of the namespaces specification is to allow the use of documents mixing elements and attributes from different vocabularies. W3C XML Schema lets you take full advantage of this possibility.

Part of the library vocabulary describes persons. This could be reused by other applications, and we might want to define a specific namespace and give it the 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="b0836217462" available="yes">
    <isbn>
      0836217462
    </isbn>
    <title>
      Being a Dog Is a Full-Time Job
    </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>
          bold, brash and tomboyish
        </ppl:qualification>
      </ppl:person>
      <ppl:person id="Snoopy">
        <ppl:name>
          Snoopy
        </ppl:name>
        <ppl:born>
          1950-10-04
        </ppl:born>
        <ppl:qualification>
          extroverted beagle
        </ppl:qualification>
      </ppl:person>
      <ppl:person id="Schroeder">
        <ppl:name>
          Schroeder
        </ppl:name>
        <ppl:born>
          1951-05-30
        </ppl:born>
        <ppl:qualification>
          brought classical music to the Peanuts strip
        </ppl:qualification>
      </ppl:person>
      <ppl:person id="Lucy">
        <ppl:name>
          Lucy
        </ppl:name>
        <ppl:born>
          1952-03-03
        </ppl:born>
        <ppl:qualification>
          bossy, crabby and selfish
        </ppl:qualification>
      </ppl:person>
    </characters>
  </book>
</library>

To handle these two namespaces, we need to define two different schemas (one per namespace). One will describe our vocabulary about persons as well as include the definitions of the element person and its child elements:

<?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 the namespace "http://dyomedea.com/ns/people", and this vocabulary doesn't include anything from any other namespace. The schema is then similar to the examples we've seen in this chapter so far. It can be used alone with documents that use only this namespace, but can also be "imported" by schemas that describe other namespaces but would like to use some of its definitions.

To do this, the schema that describes the including vocabulary needs two pieces of information. It must have a prefix for the namespaces that will be included; this is done by a usual namespace declaration. It also must have a hint on where it can find the schema for this namespace; this is done using a xs:import element:

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

The schema now has all the information it needs to resolve references to schema components that belong to the http://dyomedea.com/ns/people namespace. References can be made just using its prefix:

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

A full schema for the library vocabulary can 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>
TIP: When importing schemas from other namespaces, we can only refer to the global components defined in the imported schemas.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.