Namespaces in XML

Check Namespaces in XML for more information.

Declaring namespaces for elements

An XML namespace is a group of element and attribute names that belong to or describe a particular document style. Within a namespace, each of the names must be unique.

Namespace declarations fall into two types: default and prefixed.

Default namespaces

A default namespace declaration has the following syntax:

xmlns="URI"

A default namespace declaration applies to an element and all its descendants, unless the descendants have their own namespace declarations. If the root element has a namespace declaration, all the elements within this document that do not have an explicit declaration belong to this namespace.

The role of the URI is solely for providing an unique ID to the namespace.

Example:
<order xmlns="http://cs2.uwsuper.edu/sb/test">

Prefixed namespaces

A prefixed namespace declaration has the following syntax:

xmlns:prefix="URI"

Within a prefixed namespace an element must contain the prefix name in its start and end tags in order to belong to the namespace:

<prefix:element> . . . </prefix:element>
Example:
<agenda:meeting xmlns:agenda="http://cs2.uwsuper.edu/sb/agenda">
  <agenda:topic>Preliminaries</agenda:topic>
  <room_no>Sund 332</room_no>
</agenda:meeting>

In the above example the element room_no does not belong to the namespace agenda.

Namespaces in attributes

An attribute can be qualified by adding a namespace prefix like an element.

<prefix:element prefix:attribute="value"> . . . </prefix:element>
Example:
<mod:model xmlns:mod="http://cs2.uwsuper.edu/sb/test" mod:id="pr205">
  . . .
</mod:model>

Namespaces in style sheets

Declaring a namespace

One cannot use the above colon syntax in namespaces, because this character is reserved for pseudo-elements and pseudo-classes. Instead, you should use the following general syntax:

@namespace prefix url(URI);

Both the prefix and the URI must match the ones in the XML document.

Example:
@namespace mod url(http://cs2.uwsuper.edu/sb/test);

If the namespace prefix is omitted, then the URI in the @namespace rule is considered to be the default one.

Any @namespace rules must come after all @import and @charset rules and before any style declarations. If a @namespace prefix is declared more than once, only the last instance is taken into effect.

Applying a namespace to a selector

Ones a namespace is declared, you can apply it to each CSS selector by using the syntax:

prefix|selector {attribute1:value1; attribute2:value2; . . .}
Example:
mod|title {width: 150px;}

You can also use the wildcard symbol * with an obvious meaning:

Examples:
mod|* {font-family: Helvetica;}
*|title {width: 150px;}

Defining namespaces with the Escape character (IE only)

Not all browsers support the @namespace rule. An approach implemented in the Internet Explorer is to use the following syntax instead:

prefix\:selector {attribute1:value1; attribute2:value2; . . .}
Example:
mod\:title {width: 150px;}

This syntax is not supported by Firefox, Netscape, and Opera browsers.

Cross-browser definition of namespaces

As of today, the following trick works fine for combining two namespaces:

  1. Use the default namespace for one of them, no other changes in the document are needed.
  2. Use two CSS files for styling separately the prefixed namespace and the default one.
  3. In each CSS file still use the @namespace rule to define the prefixed namespace (IE ignores it, all other browsers really need it).
  4. In the prefixed CSS file duplicate EACH selector, ones for the IE syntax and the second time for the W3C syntax.
Example:
mod\:title {width: 150px;}
mod|title {width: 150px;}

Check how it works. Do you know a simpler solution?