XML technologies / XML Schema / Controlling namespaces / Chameleon design

(Excerpt from “XML Schema” by Eric van der Vlist)

This chapter would be incomplete without examining how namespaces affect inclusion through xs:include and xs:redefine.

Initially, we ignored namespaces when introducing these constructs. Yet they interact with namespaces because W3C XML Schema restricts inclusion or redefinition to schemas with the same target namespace or none. Schemas for other languages are imported instead of included.

When a schema fragment with no target namespace is included in one with a target, its definitions adopt the including schema’s namespace as if originally defined there. This enables namespace-transparent fragment libraries adopting their host’s namespace. It is often called chameleon design because the included schema takes on its surroundings’ color.

To illustrate this and its implications, revisit our chapter’s example:

<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library">
   <book id="b3810518883">
      <title>
         Auf den Hund gekommen
      </title>
      <authors>
         <person id="CMS">
            <name>
               Charles M. Schulz
            </name>
         </person>
      </authors>
   </book>
</library>

A library of fragments describing people for different vocabularies can use a fragment without target namespace:

<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="person">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="name" type="xs:string"/>
         </xs:sequence>
         <xs:attribute name="id" type="xs:string" use="required"/>
      </xs:complexType>
   </xs:element>
</xs:schema>

The including schema would be:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" xmlns="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:include schemaLocation="very-simple-1-ns-ppl.xsd"/>
   <xs:element name="library">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="book">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="title" type="xs:string"/>
                     <xs:element name="authors">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element ref="person"/>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
                  <xs:attribute name="id" type="xs:ID"/>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

This looks simple and clean, but is it truly modular? Including schema fragments resembles including C headers. It improves slightly on copy/paste, but offers limited modularity.

Element person has our library’s namespace. An application inspecting the instance cannot infer its identity with a person element in another document, for example describing an employee.

Inspecting the modified infoset and type does not help: its type belongs to our library’s target namespace and will not match other namespaces including the same fragment.

The fact that the same element person is used by different vocabularies is lost entirely during inclusion or redefinition. Consider whether a separate namespace would better preserve information in the instance, or at least in the modified infoset, by importing data types from another namespace rather than including shared definitions.

   

 

 

 

back to top