SCDJWS Study Guide: XML Namespace

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Declaring Namespaces


[Definition:] A namespace is declared using a family of reserved attributes. Such an attribute's name must either be xmlns or have xmlns: as a prefix. These attributes, like any other XML attributes, may be provided directly or by default.

Attribute Names for Namespace Declaration




PrefixedAttName | DefaultAttName




'xmlns:' NCName


NSC: Leading "XML"]










(Letter |'_') (NCNameChar)*


An XML Name, minus the ":" */




Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender



[Definition:] The attribute's value, a URI reference, is the namespace name identifying the namespace. The namespace name, to serve its intended purpose, should have the characteristics of uniqueness and persistence. It is not a goal that it be directly usable for retrieval of a schema (if any exists). An example of a syntax that is designed with these goals in mind is that for Uniform Resource Names [RFC2141]. However, it should be noted that ordinary URLs can be managed in such a way as to achieve these same goals.

[Definition:] If the attribute name matches PrefixedAttName, then the NCName gives the namespace prefix, used to associate element and attribute names with the namespace name in the attribute value in the scope of the element to which the declaration is attached. In such declarations, the namespace name may not be empty.

[Definition:] If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that of the default namespace in the scope of the element to which the declaration is attached. In such a default declaration, the attribute value may be empty.

An example namespace declaration, which associates the namespace prefix edi with the namespace name

<x xmlns:edi=''>
<!-- the "edi" prefix is bound to
for the "x" element and contents -->

Namespace Constraint: Leading "XML"

Prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and XML-related specifications.

Default Namespaces

A default namespace declaration uses the following syntax:

xmlns ='<namespace identifier>'

Notice that there is not prefix. Defining a default namespace for an element saves us from using prefixes in all the child elements.

All unqualified child element names of the element which defines the default namespace are automatically associated with the specific namespace identifier. The default namespace declarations, however, have absolutely no effect on attributes. To apply a namespace to an attribute, the only way is through a prefix (i.e., explicit qualified).

This XML document contains information in a table:

<table xmlns="">

In the above document, the default namespace identifier for the <table>, <tr>, and <td> elements is "".

The default namespace can be set to the empty string. This has the same effect, within the scope of the declaration, of there being no default namespace.

NONAMESPACE (undeclaring namespace) declaration

The following namespace declaration is known as the NONAMESPACE declaration:

xmlns =""

The containing element and its child elements are declared to be in no namespace. An element that is in the NONAMESAPCE namespace has its namespace prefix and URI set to empty strings.

<d:student xmlns:d=''
        xmlns='urn:foo' id='3235329' >
<name>John Smith</name>
<language xmlns="" >C#</language>

Here, "student" is from the namespace while "name" and "score" are from the default namespace urn:foo. The id attribute doesn't belong to a namespace since attributes aren't automatically associated with the default namespace identifier.

This example also illustrates that you can undeclare a default namespace by simply setting the default namespace identifier back to the empty string, as shown in the language element (remember you cannot do this with prefix declarations in XML 1.0). As a result, the language element also doesn't belong to a namespace.

Here's an invalid example of unbinding a prefix per Namespaces in XML 1.0 spec, but a valid example per Namespaces in XML 1.1:

<language xmlns:lang="">

From this point on, the prefix lang cannot be used in the XML document because it is now undeclared as long as you are in the scope of element language. Of course, you can definitely re-declare it.

No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared explicitly using xmlns. When a {default namespace} has not been declared at all using xmlns, it is incorrect to say that the elements are in {default namespace}. In such cases, we say that the elements are in {no namespace}. {no namespace} also applies when an already declared {default namespace} is undeclared.

If no default namespace is explicitly declared, as in the following XML document, then the URI associated with the default namespace is the current document.

<?xml version="1.0"?>
<X:a xmlns:X="">
<b>this is a test</b>

It is possible, however, to set up a default namespace whose URI is some other document. To do that, use a namespace declaration with a URI but no prefix.

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |