Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
October 31, 1997
Introduction
What a Namespace Is and Is Not
In the DTD
In the Instance
Unqualified Names
Validation
XML Object Model
References
Currently, XML documents use names (e.g., element names, attribute names, etc.) whose meaning is associated with the document's document type. The DTD serves both to define a class of document and also a namespace (all the names that can be used within the document type). There are no simple, standardized means to express a document type that uses names from several namespaces. This proposes a conceptual model and concrete syntax for an implementation of namespaces, allowing DTDs to use names from several spaces. Document instances, also, may use names from several spaces, while being self-describing regarding the namespaces they employ (that is, the use of a namespace is not dependent on having read the DTD).
Briefly, a namespace has a defining document, such as a DTD. The document defines a number of names (e.g., elements, attributes, entities, etc.). The document can be reached via a URI; this URI is the identifier of the namespace. All particular names are qualified; that is, they consist of two parts: the namespace's identifier and the specific name within the namespace (e.g., the URI to the DTD and the element name within the DTD). Every qualified names is globally unique, and every specific name has only one fully qualified form. These qualified names can be used in DTDs and also in document instances. Various syntactic devices are used to implement this conceptual model within XML.
A namespace is a controlled set of names. It is simply a mechanism to make sure that names are globally unique, and to allow any reader of a document to find the globally unique, fully qualified form of each name. It has no special provisions for associating semantics with names. A namespace per se does not imply any special meaning to the names defined in it. It has nothing special to do with multiple inheritance, formatting, versioning, brevity, or anything besides making sure that names are globally unique and globally defined.
We reserve and pre-define the special processing instruction called "XML:NAMESPACE" that will be recognized by the XML parser. In use, it has the form:
<?XML:NAMESPACE HREF="uri" AS="ns"?>
The HREF attribute contains a URI. This instruction associates ns with uri, such that ns can be used as a stand-in for uri as the namespace's identifier (the first part of a qualified name). This makes it valid to use names of the form ns:specific-name anywhere that XML allows a name, with the meaning that the name is to be interpreted as fully qualified with the two parts, uri and specific-name. For example:
<?XML:NAMESPACE HREF="http://zoo.org/schema.dtd" AS="Zoo"?> <!ELEMENT AnimalFriends (Zoo:Animal*) >
This makes it valid to use, within the content model of AnimalFriends, elements called "Animal" from the http://zoo.org/schema.dtd namespace.
An XML instance has available to it all namespaces declared in its DTD, as above. They can be used with the same syntax and meaning as employed in the DTD; for example:
<!DOCTYPE AnimalFriends SYSTEM "af.dtd"> <AnimalFriends> <Zoo:Animal>Bear</Zoo:Animal> <Zoo:Animal>Meerkat</Zoo:Animal> </AnimalFriends>
In addition, to allow XML instances to be self-describing regarding their fully qualified names, the XML:NAMESPACE processing instruction can be used in an XML instance. This introduces the namespace into the scope of its containing element, such that ns can be used as to qualify any peer element following it. A namespace introduced into a scope overrides, for that scope, any same-named namespace in an enclosing scope.
<!DOCTYPE AnimalFriends SYSTEM "af.dtd"> <AnimalFriends> <?XML:NAMESPACE HREF="http://zoo.org/schema.dtd" AS="Zoo"?> <Zoo:Animal>Bear</Zoo:Animal> <Zoo:Animal>Meerkat</Zoo:Animal> </AnimalFriends>
This only works, of course, if the Content Model for the AnimalFriends element in the af.dtd includes the ANY keyword. The scope of the Zoo namespace is confined to the element in which it is declared -- which, in this case, is the AnimalFriends element.
All names in a document have a fully qualified form. The outermost element in a document is always assumed to come from the document's DTD namespace, and does not need to be explicitly qualified. For contained elements, the explicit namespace and ":" can also be omitted. If so, the enclosing element's namespace is inferred (for sub-elements, attributes, etc.). These rules ensure that all names in all existing XML documents have fully qualified names matching their current meaning, and all validations remain unchanged.
Here is an example of explicit and inferred namespaces:
<?xml version="1.0" RMD="NONE"?> <!DOCTYPE AnimalFriends SYSTEM "af.dtd"> <AnimalFriends> <?XML:NAMESPACE HREF="http://zoo.org/schema.dtd" AS="Zoo"?> <?XML:NAMESPACE HREF="http://foodstore.com/schema.dtd" AS="Food"?> <Zoo:Animal>Bear</Zoo:Animal> <Zoo:Animal Size="small" Food:Eats="crickets"> <CuteThing>Meerkat</CuteThing></Zoo:Animal> </AnimalFriends>
Here, Size and CuteThing are qualified to the http://zoo.org/schema.dtd namespace, while Eats is qualified to http://foodstore.com/schema.dtd.
If a qualified name is used but the namespace is not declared, the document becomes invalid (but remains well-formed). When a document is validated, all elements must be validated against their originating DTDs. Namespaces referenced in an XML:NAMESPACE processing instruction will be used for validation if the XMLDecl at the beginning of the document contains the attribute RMD="ALL". For example, the following is fully validated:
<?xml version="1.0" RMD="ALL"?> <!DOCTYPE AnimalFriends [ <?XML:NAMESPACE HREF="http://zoo.org/schema.dtd" AS="Zoo"?> <!ELEMENT AnimalFriends (Zoo:Animal*) > <XML:NAMESPACE HREF="http://foodstore.com/schema.dtd" AS="Food"/> <!ATTLIST Zoo:Animal Food:Eats CDATA #IMPLIED> ]> <AnimalFriends> <Zoo:Animal>Bear</Zoo:Animal> <Zoo:Animal Size="small" Food:Eats="crickets"> <CuteThing>Meerkat</CuteThing></Zoo:Animal> </AnimalFriends>
The XML object model stores Name objects for element and attribute names. These names can be either simple names or qualified names, depending on the use of namespaces in the XML document. In the above example, the Name returned from the getTagName() method on the AnimalFriends elements is a simple name that has no namespace. The Name returned for the Zoo:Animal elements, however, is qualified, and the getNameSpace() method returns "http://zoo.org/schema.dtd". The implicit names are also fully resolved, so the getNameSpace() on the tag name for the CuteThing element also returns "http://zoo.org/schema.dtd".
(Please note that the links below require W3C passwords.)
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.