3.5 XML Namespaces

The intent of XML namespaces is to eliminate naming conflicts in XML documents that contain element types and attributes from multiple XML languages. Namespaces can prove useful in situations in which documents combine components from several fields that have the same name but are used in a different context. For example, the bird classification document shown previously in Example 3-6 uses the element <name> to identify a particular species of heron:

 <name language="latin">Ardea herodias</name> <name language="english">Great Blue Heron</name> 

The <name> element we saw in Example 3-8 identifies the contributing authors of an article:

 <name>George W. Archibald</name> <name>James C. Lewis</name> 

Each of the name elements has a different content model. In this case, the document that contains names of birds can use "name" qualified by one Uniform Resource Identifier (URI; see Chapter 7) [RFC 2396]. Figure 3-6 shows the parts of a typical HTTP URI as used in a namespace. A document that contains names of authors can use "name" qualified by another URI. The URI does not have to point to an actual file or other resource.

Figure 3-6. Parts of a namespace as shown in the form of a URI



XML namespaces do not have any official function except providing a two-part naming system for element types and attributes. They may, however, point to a schema, text document, or other useful information.

XML namespaces are collections of names identified by a URI. You use an XML namespace in XML documents to qualify element and attribute names. A unique two-part name identifies the element and attribute names in the namespace. The XML Namespaces Recommendation [Names] defines a mapping based on the idea of a prefix. When an element type name or attribute name contains a colon, then the mapping treats the part of the name before the colon as a namespace prefix. The part following the colon gives the local name.


Uniform Resource Identifiers [RFC 2396] are a key Web concept, making it almost inevitable that they would be employed by the W3C as namespace identifiers. This approach has both advantages and disadvantages:

  • The URI can be chosen so that it points to something useful, such as a DTD or schema. This relationship is not required, however, and some insist that namespace names should be opaque identifiers without external reference.

  • The hierarchical allocation of the domain names that occur in most URIs makes it relatively easy to assure namespace uniqueness.

  • The rich structure of most URIs (thanks to their path, fragment, query, and other sections) provides many dimensions of flexibility. Unfortunately, it can also lead to complexity and verbosity.

The prefix maps to a URI reference and selects a namespace. The combination of the universally managed URI namespace and the document's own namespace produces universally unique identifiers. The namespace prefix serves as a proxy for a URI reference.


The Namespaces in XML Recommendation [Names] assigns a meaning to names containing colon characters. Authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character.

3.5.1 Namespace Declarations

To declare a namespace, you use an attribute, either "xmlns" as the attribute name or "xmlns:" as a prefix to the attribute name. The attribute value is a URI and identifies the namespace (see Example 3-10). Chapter 5 also provides more information about namespace declarations.

Example 3-10 Associating a namespace prefix with the namespace name
 <?xml version="1.0"?> <name xmlns:family='http://myhomepage.com/classification'>   <!-- the "family" prefix is bound to        http://myhomepage.com/classification for the "name"        element and contents --> 


XML and XML-related specifications reserve for their use prefixes beginning with the three-letter sequence x, m, l, in any case combination [XML].

The prefix provides the namespace prefix part of the qualified name and must have been associated with a namespace URI reference by a namespace declaration. The portion following the prefix provides the local part of the qualified name. The prefix functions as a placeholder for the namespace name.

  • The prefix "xml" is by definition bound to the namespace name:

  • The prefix "xmlns" is used only for namespace bindings and is not itself bound to any namespace name.

3.5.2 Using Qualified Names

Element names are given as qualified names. Attributes may be namespace declarations, or their names may be given as qualified names.

The following two examples come from the Namespaces in XML Recommendation [Names]. Here the namespace declaration applies to the element where it is specified and to all elements within the content of that element. This relationship holds true unless it is overridden by another namespace declaration with the same prefix:

 <?xml version="1.0"?> <!-- all elements here are explicitly in the HTML namespace --> <html:html xmlns:html='http://www.w3.org/TR/REC-html40'>   <html:head>     <html:title>Frobnostication</html:title>   </html:head>   <html:body><html:p>Moved to     <html:a href='http://frob.com'>here.</html:a></html:p>   </html:body> </html:html> 

You can declare multiple namespace prefixes as attributes of a single element:

 <?xml version="1.0"?> <!-- both the bk and isbn namespace prefixes     are available throughout --> <bk:book xmlns:bk='urn:loc.gov:books'          xmlns:isbn='urn:ISBN:0-395-36341-6'>     <bk:title>Cheaper by the Dozen</bk:title>     <isbn:number>1568491379</isbn:number> </bk:book> 

3.5.3 Namespace Guidelines

Keep in mind the following guidelines when using namespaces:

  • No tag may contain two attributes that have identical names or have qualified names with the same local part and with prefixes that have been bound to identical namespace names.

  • Documents using namespaces must conform to the "Namespace Constraints" described in the Namespaces for XML Recommendation [Names]. For example:

    • All element types and attribute names must contain either zero or one colon.

    • No entity names, processing instruction targets, or notation names can contain any colons.

  • Namespace URIs are simply identifiers. The URIs in XML namespace names can, but do not necessarily, point to schemas, information about the namespace, or anything else.

  • Ronald Bourret, in XML Namespaces Frequently Asked Questions [Bourret], states that namespaces do not provide or define any of the following:

    • A way to merge two documents that use different DTDs

    • A way to associate XML namespaces and schema information

    • A way to validate documents that use XML namespaces

    • A way to associate element type or attribute declarations in a DTD with an XML namespace

Secure XML(c) The New Syntax for Signatures and Encryption
Secure XML: The New Syntax for Signatures and Encryption
ISBN: 0201756056
EAN: 2147483647
Year: 2005
Pages: 186

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net