XML technologies / XML Schema / Controlling namespaces / Importing schemas without namespaces
(Excerpt from “XML Schema” by Eric van der Vlist)
We have seen how to reference components from other namespaces, and how their elements and attributes retain their full names, namespaces and forms; see “To qualify or not to qualify—that is the question”. However, only global elements and attributes can be referenced, and these must always be qualified.
The only solution for importing elements and attributes without a namespace is therefore to import a schema with no target namespace. Suppose we want to describe a document whose vocabulary for people has no namespace:
<?xml version="1.0"?>
<lib:library xmlns:lib="http://dyomedea.com/ns/library">
<lib:book id="b3810518883">
<lib:title>
Auf den Hund gekommen
</lib:title>
<lib:authors>
<person id="CMS">
<name>
Charles M. Schulz
</name>
</person>
</lib:authors>
</lib:book>
</lib:library>Importing the schema without a namespace works just as for schemas with target namespaces, except that attribute namespace is omitted. Also, the default namespace, without a prefix, must be available to reference components from the included 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:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="very-simple-2-ns-ppl-nons.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="person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>The included schema is:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person" type="personType"/>
<xs:complexType name="personType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>All components are considered unqualified here. All other behavior, including the difference between element or attribute references and the use of data types, remains relevant.
When defining unqualified elements and attributes in a schema with a target namespace, we must therefore choose whether to define them locally there or in a separate imported schema without a target namespace.
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