The chameleon schema takes the “color” (namespace) of the main schema

Suppose you have several schemas which use common elements like a table or a certain type (in this example the nameType).

1
2
3
4
5
6
7
8
9
10
The document instance
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="testSchema test.xsd" xmlns="testSchema">
  <person>
    <name>
      <firstname></firstname>
      <lastname></lastname>
    </name>
  </person>
</test>

The target namespace of the document instance is testSchema.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The main schema test.xsd with targetNamespace testSchema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="testSchema" targetNamespace="testSchema"
  elementFormDefault="qualified">

  <xsd:include schemaLocation="common.xsd" />

  <xsd:element name="test">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="person" maxOccurs="unbounded" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="person">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="name" type="nameType" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

The main schema (test.xsd) includes the chameleon schema (common.xsd) which has no namespace and inherits the namespace of the main schema “testSchema”.

1
2
3
4
5
6
7
8
9
10
11
12
13
The chameleon schema "common.xsd"
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="nameType">
    <xsd:sequence>
      <xsd:element ref="firstname"/>
      <xsd:element ref="lastname"/>
    </xsd:sequence>
  </xsd:complexType>
 
  <xsd:element name="firstname"/>
  <xsd:element name="lastname"/>

</xsd:schema>

The previous examples were tested with Eclipse.

Rating 3.00 out of 5
[?]