XML technologies / XML Schema / Controlling namespaces / Namespace behavior of imported components
(Excerpt from “XML Schema” by Eric van der Vlist)
You may wonder why namespaces for schema-defined elements are identified differently from the approach used by W3C XML Schema for data types. Importing schemas restricts namespace usage in the imported schema.
Defining an element or attribute assigns it a namespace, which must match the importing schema’s target even if its data type belongs to another namespace.
Referencing a component, not a data type, from another namespace follows different rules: its name is imported, and its namespace must be the imported schema’s target.
To illustrate this, examine two ways of creating schemas in this example:
<?xml version="1.0"?>
<!-- Namensraum: http://dyomedea.com/ns/library -->
<library xmlns:ppl="http://dyomedea.com/ns/people" xmlns="http://dyomedea.com/ns/library">
<book id="b3810518883">
<title>
Auf den Hund gekommen
</title>
<authors>
<ppl:person id="CMS">
<ppl:name>
Charles M. Schulz
</ppl:name>
</ppl:person>
</authors>
</book>
</library>This document contains two namespaces. Everything except authors element contents is in namespace "http://dyomedea.com/ns/library". Contents of element authors (ppl:person and ppl:name) belong to namespace "http://dyomedea.com/ns/people".
We have two main ways to describe this using W3C XML Schema. Both start by defining a schema for elements in namespace "http://dyomedea.com/ns/library". The first imports the schema defining namespace "http://dyomedea.com/ns/people", then references element ppl:person for use within element authors:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ppl="http://dyomedea.com/ns/people" xmlns:lib="http://dyomedea.com/ns/library">
<xs:import namespace="http://dyomedea.com/ns/people" schemaLocation="very-simple-2-ns-ppl.xsd"/>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="lib:bookType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element ref="ppl:person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>The second uses the same import but defines element authors with type ppl:authorType instead of explicitly defining its complex type, producing a shorter schema:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:ppl="http://dyomedea.com/ns/people" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://dyomedea.com/ns/people" schemaLocation="very-simple-2-ns-ppl.xsd"/>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="lib:bookType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="authors" type="ppl:authorType"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>Both validate the same documents but differ stylistically. Applications using schema information see the document differently. The first separates namespaces more clearly: referencing lets element ppl:person appear within element authors without directly mixing element authors into namespace "http://dyomedea.com/ns/people". The shorter second approach assigns a type from one namespace to an element in another.
For validation alone, the distinction is irrelevant: both validate exactly the same documents. But applications using type information through the PSVI, or schema-based binding at compile time, may find the perspective shift significant. The type approach requires applications to understand more schema content and creates new dependencies on details of W3C XML Schema processing.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top