This example shows the difference between including and importing an XML schema and the purpose of it.
I kept the examples small just to understand the principles.

But first: why namespaces?

Namespaces are used to prevent collisions between content models.
Suppose you have two “Johns”, how to make the difference? Well give them a family name like:

  • John Deere
  • John Trudell

Now we have a Deere namespace and a Trudell namespace. The same principle is used in XML Schemas.

Why import or include schemas?

This is done to make your schemas modular and reusable.
Including brings in definitions belonging to the same targetNamespace.
Importing brings in definitions belonging to a different targetNamespace.

The house example and its schemas

I have a master schema house.xsd, which imports the garage.xsd schema and the kitchen.xsd schema.

house.xsd schema
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3.  targetNamespace="http://www.example.org/house"
  4.  elementFormDefault="qualified"
  5.  xmlns:kit="http://www.example.org/kitchen"
  6.  xmlns:gar="http://www.example.org/garage">
  7.  
  8.  <import schemaLocation="kitchen.xsd" namespace="http://www.example.org/kitchen" />
  9.  <import schemaLocation="garage.xsd" namespace="http://www.example.org/garage" />
  10.  
  11.  <element name="house">
  12.   <complexType>
  13.    <sequence>
  14.     <element ref="kit:kitchen" />
  15.     <element ref="gar:garage" />
  16.    </sequence>
  17.   </complexType>
  18.  </element>
  19. </schema>

targetNamespace of the house.xsd schema: http://www.example.org/house
targetNamespace of the garage.xsd schema: http://www.example.org/garage
targetNamespace of the kitchen.xsd schema: http://www.example.org/kitchen

kitchen.xsd schema
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3.  targetNamespace="http://www.example.org/kitchen"
  4.  xmlns:tns="http://www.example.org/kitchen"
  5.  elementFormDefault="qualified">
  6.  
  7.  <include schemaLocation="light.xsd" />
  8.  
  9.  <element name="kitchen">
  10.   <complexType>
  11.    <sequence>
  12.     <element ref="tns:floor" />
  13.     <element ref="tns:ceiling" />
  14.     <element ref="tns:light" />
  15.    </sequence>
  16.   </complexType>
  17.  </element>
  18.  
  19.  <element name="floor" />
  20.  <element name="ceiling" />
  21. </schema>
garage.xsd schema
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/garage"
  4. xmlns:tns="http://www.example.org/garage"
  5. elementFormDefault="qualified">
  6.  
  7.  <include schemaLocation="light.xsd"/>
  8.  
  9.  <element name="garage">
  10.   <complexType>
  11.    <sequence>
  12.     <element ref="tns:floor" />
  13.     <element ref="tns:ceiling" />
  14.     <element ref="tns:light"/>
  15.    </sequence>
  16.   </complexType>
  17.  </element>
  18.  
  19.  <element name="floor" >
  20.   <complexType>
  21.    <sequence>
  22.     <element name="color"/>
  23.    </sequence>
  24.   </complexType>
  25.  </element>
  26.  <element name="ceiling" />
  27. </schema>

The schemas garage.xsd and kitchen.xsd both have the same elements floor and ceiling but that’s no problem because they live in their own namespace! The floor element has also a different content model.

Both schemas include the light.xsd schema.

light.xsd schema, no target namespace here!
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema">
  3.  <element name="light"/>
  4. </schema>

The light.xsd schema has no targetNamespace, so when included it takes over the targetNamespace of the including schema (http://www.example.org/garage and http://www.example.org/kitchen).

The house document instance

house document instance
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <house xmlns="http://www.example.org/house"
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  xsi:schemaLocation="http://www.example.org/house house.xsd ">
  5.  <kitchen xmlns="http://www.example.org/kitchen">
  6.   <floor></floor>
  7.   <ceiling></ceiling>
  8.   <light></light>
  9.  </kitchen>
  10.  <garage xmlns="http://www.example.org/garage">
  11.   <floor>
  12.    <color></color>
  13.   </floor>
  14.   <ceiling></ceiling>
  15.   <light></light>
  16.  </garage>
  17. </house>

All examples were made and tested with Eclipse.

Rating 3.00 out of 5
[?]