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).

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

The target namespace of the document instance is testSchema.

The main schema test.xsd with targetNamespace testSchema
  1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  2.  xmlns="testSchema" targetNamespace="testSchema"
  3.  elementFormDefault="qualified">
  4.  
  5.  <xsd:include schemaLocation="common.xsd" />
  6.  
  7.  <xsd:element name="test">
  8.   <xsd:complexType>
  9.    <xsd:sequence>
  10.     <xsd:element ref="person" maxOccurs="unbounded" />
  11.    </xsd:sequence>
  12.   </xsd:complexType>
  13.  </xsd:element>
  14.  
  15.  <xsd:element name="person">
  16.   <xsd:complexType>
  17.    <xsd:sequence>
  18.     <xsd:element name="name" type="nameType" />
  19.    </xsd:sequence>
  20.   </xsd:complexType>
  21.  </xsd:element>
  22.  
  23. </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”.

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

The previous examples were tested with Eclipse.

Rating 3.00 out of 5
[?]