XML

Including Schemas in the Same targetNamespace

We could write a similar schema with a namespace to identify schema elements. For example, let's create a schema named NorthwindMessage.xsd, as shown here:

  <schema targetNamespace="http://www.northwindtraders.com/Message"
      xmlns:northwindMessage="http://www.northwindtraders.com/Message"
      xmlns ="http://www.w3.org/1999/XMLSchema">
  <include schemaLocation=
      "http://www.northwindtraders.com/HTMLMessage.xsd"/>
      <element name="doc">
          <group>
              <option>
                  <element ref="northwindMessage:body"/>
                  <element ref="northwindMessage:HTMLbody"/>
              </option>
          </group>
      </element>
      <element name="body">
          <attribute name="bodyText" type="northwindMessage:TextBody"/>
      </element>
  </schema>
NOTE
The schema namespace is not assigned a prefix, so it is the default namespace. All elements without a prefix will belong to the schema namespaces. When elements that are defined in the schema are used, the schema's namespace must be used, as was done with the body and HTMLbody elements. In schemas, the body and HTMLbody elements can be separated from their namespace prefixes and properly identified.

The included file, HTMLMessage.xsd, would look like this:

  <xsd:schema targetNamespace:xsd="http://www.northwindtraders.com/    Message"
      xmlns:northwindMessage="http://www.northwind.com/Message"
      xmlns ="http://www.w3.org/1999/XMLSchema">
      <xsd:simpleType name="TextBody" base="string"
          minLength="0"
          maxLength="20"/>
      <xsd:element name="HTMLbody">
          <xsd:attribute name="bodyText" type="string"/>
      </xsd:element>
  </xsd:schema>
NOTE
In this case, we did assign a prefix to the schema namespace and used this prefix throughout the document. You can use either method, but keep in mind that defaults can sometimes be harder for people to interpret.

As you can see, namespaces play a major role in schemas. Let's look at the different elements included in this example. Both documents include a targetNamespace. A targetNamespace defines the namespace that this schema belongs to (http://www.northwindtraders.com/Message). Remember, a namespace uses a URI as a unique identifier, not for document retrieval (although an application could use the namespace to identify the associated schema). It will be up to the application to determine how the namespace is used. The include element has a schemaLocation attribute that can be used by an application or a person to identify where the schema is located.

The HTMLMessage.xsd file is included in the NorthwindMessage.xsd by using the include element. For one schema to be included in another, both must belong to the same namespace. Using the include element will result in the included document being inserted into the schema in place of the include element. Once the insertion is complete, the schema should still be a well-formed XML document. A top-level schema can be built that includes many other schemas.

Notice that the simpleType TextBody is declared in the included document but used in the top-level documents. This separation makes no difference, as both documents will be combined into one document by the processing application.

The XML document that is based on the schema is referred to as an instance document. This instance document will have only a reference to the top-level schema. The instance document will need to use only the namespace of the top-level schema. Thus, the instance document for our example schema would look like this:

  <?xml version="1.0"?>
  <northwindMessage:doc xmlns: northwindMessage=
      "http://www.northwindtraders.com/Message" >
      <body bodyText="Hello, world"/>
  </northwindMessage:doc>

You could also have the following instance document:

  <?xml version="1.0"?>
  <northwindMessage:doc xmlns:northwindMessage=
      "http://www.northwindtraders.com/Message">
      <HTMLbody bodyText="<h1>Hello, world</h1>"/>
  </northwindMessage:doc>

As far as the instance document is concerned, all the elements come from the top-level schema. The instance document is unaware of the fact that HTMLbody element actually comes from a different schema because the schema resolves all the different namespaces.