Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Tip: Work with schemas and namespaces

Referencing multiple schemas from XML, and from other XML schemas

Brett McLaughlin (brett@newInstance.com), Author and Editor, O'Reilly Media Inc.
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.

Summary:  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.

View more content in this series

Date:  01 Sep 2002
Level:  Intermediate

Comments:  


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.


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.


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.


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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12154
ArticleTitle=Tip: Work with schemas and namespaces
publish-date=09012002
author1-email=brett@newInstance.com
author1-email-cc=htc@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).