Skip to main content

skip to main content

developerWorks  >  XML  >

Tip: Work with schemas and namespaces

Referencing multiple schemas from XML, and from other XML schemas

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Intermediate

Brett McLaughlin (brett@newInstance.com), Author and Editor, O'Reilly Media Inc.

01 Sep 2002

This tip explains how the XML Schema specification requires one XML Schema per namespace, and shows you how to include more than one schema for a document and how to reference one schema from another.

With the advent of XML namespaces, it has become common to see XML documents with one, two, or even more XML namespaces. Content authors use multiple namespaces to segregate content and to validate specific portions of a document against one set of constraints, and other portions against another constraint set. However, with XML namespaces, the XML Schema specification has added some additional complexity to the equation. While XML Schema allows a much richer model for detailing the content of an element or attribute, it is easy to make mistakes when dealing with multiple namespaces and multiple schemas within the same XML document.

The CD database: An example

Before diving into details, look at a simple XML document that I use as an example for illustrating these concepts. The document in Listing 1 , while somewhat trivial, does use two different namespaces.


Listing 1. XML with two namespaces

<?xml version="1.0"?>
<database xmlns="http://www.newInstance.com/cd-database"
          xmlns:sh="http://www.sugarhillrecords.com"
>
 <cd title="Nickel Creek">
  <artist>Nickel Creek</artist>
  <sh:album><
   <sh:description>Bluegrass Revivalists, Acoustic Innovators, 
    Youthgrass are just some of the terms that have been 
    used to describe Nickel Creek over the past year -- perhaps 
    producer Alison Krauss says it best:It's just Nickel Creek 
    music.</sh:description>
   <sh:producer>Alison Krauss</sh:producer>
  </sh:album>
 </cd>
 <cd title="Ice Caps: Peaks of Telluride">
  <artist>Sam Bush</artist>
  <sh:album>
   <sh:description>Sam Bush's amazing mandolin and fiddle 
    playing, singing and outrageous stage presence have 
    been captivating audiences since the late 1960s. So what 
    could be better-or more fun-than a live Sam Bush album 
    recorded at the Telluride Bluegrass Festival, where he 
    has performed for 26 years?
   </sh:description>
   <sh:producer>Sam Bush</sh:producer>
  </sh:album>
 </cd>
</database>


This document isn't very useful to anyone who doesn't understand what information it is allowed to contain; and for that, you need to assign a schema to each namespace used in the document. Though there are only two here, you can extrapolate the idea presented to three or more namespaces quite easily. First, you want to use the schemaLocation attribute to match up each namespace with the correct namespace. That requires the addition of another namespace as well; Listing 2 shows the results of making both of these changes.


Listing 2. Matching namespaces up with XML Schema

<?xml version="1.0"?>
<database xmlns="http://www.newInstance.com/cd-database"
  xmlns:sh="http://www.sugarhillrecords.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.newInstance.com/cd-database cd-database.xsd
                      http://www.sugarhillrecords.com sugarHill.xsd"
>
  <cd title="Nickel Creek">
    <artist>Nickel Creek</artist>
    <sh:album><
      <sh:description>Bluegrass Revivalists, Acoustic Innovators, 
        Youthgrass are just some of the terms that have been used 
        to describe Nickel Creek over the past year -- perhaps 
        producer Alison Krauss says it best:It's just Nickel Creek 
        music.</sh:description>
      <sh:producer>Alison Krauss</sh:producer>
    </sh:album>
  </cd>
  <cd title="Ice Caps: Peaks of Telluride">
    <artist>Sam Bush</artist>
    <sh:album>
      <sh:description>Sam Bush's amazing mandolin and fiddle playing, 
        singing and outrageous stage presence have been captivating 
        audiences since the late 1960s. So what could be better-or 
        more fun-than a live Sam Bush album recorded at the Telluride 
        Bluegrass Festival, where he has performed for 26 years?
      </sh:description>
      <sh:producer>Sam Bush</sh:producer>
    </sh:album>
  </cd>
</database>


So far, nothing is too difficult. The namespace URI is paired up with the XML schema that's used for that URI, and then the same thing is repeated for the next namespace/schema pair. However, things get tricky when you start working with the XML schemas themselves.



Back to top


The CD database schema

First, let's start with the schema that is going to be referenced; that will be the one for the Sugar Hill portion of the document, called sugarHill.xsd. This is the best place to start, because it will not have to reference the other schema (the reverse is not true). Listing 3 shows this schema.


Listing 3. The Sugar Hill schema

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.sugarhillrecords.com" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
>
  <xs:element name="album">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="description" type="xs:string" />
        <xs:element name="producer" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


Nothing too surprising here, right? Just your plain vanilla XML schema. However, take a look at Listing 4, which shows an in-progress version of the schema for the CD database, for the http://www.newInstance.com/cd-database namespace.


Listing 4. The CD database schema

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.newInstance.com/cd-database" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
>
  <xs:element name="cd">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="artist" type="xs:string" />
        <xs:element ref="sh:album" />
      </xs:sequence>
      <xs:attribute name="title" type="xs:string" />
    </xs:complexType>
  </xs:element>
</xs:schema>


This looks OK, but it's incomplete. While the structure is defined correctly, your XML parser will complain when it tries to parse and evaluate the CD database schema. That's because it doesn't know what to do with sh:album. The namespace prefix sh isn't assigned to a namespace URI, and it doesn't know where the schema that defines the album element is. This is actually easier to fix than you might imagine, and I'll deal with that next.



Back to top


Importing schemas

First, you need to define the Sugar Hill namespace URI; this is easy, and something you already did in the original XML document. Second, and less often understood, you need to import the other XML schema that you are using. Listing 5 shows the use of the import element (from the XML Schema definition namespace), and how it links the two XML schema documents together.


Listing 5. The CD database schema

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.newInstance.com/cd-database" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:sh="http://www.sugarhillrecords.com"
           elementFormDefault="qualified"
>
  <xs:import namespace="http://www.sugarhillrecords.com" 
       schemaLocation="sugarHill.xsd"/>
  <xs:element name="cd">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="artist" type="xs:string" />
        <xs:element ref="sh:album" />
      </xs:sequence>
      <xs:attribute name="title" type="xs:string" />
    </xs:complexType>
  </xs:element>
</xs:schema>


With these two additions, everything should now work. Now your schema references the Sugar Hill schema, and can resolve and use the album element definition. In addition, your XML document already references both schemas, so it won't have any trouble when it's parsed.



Back to top


Summary

Ideally, this tip has filled in some of the blanks about working with multiple schemas. I spent quite a bit of time trying to wade through the specifications the first time this came up in my content authoring. I hope this saves you from having to do the same. Enjoy, and I'll see you online!



Resources



About the author

Photo of Brett McLaughlin

Brett McLaughlin has been working in computers since the Logo days (Remember the little triangle?). He currently specializes in building application infrastructure using Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.




Rate this page


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top