XML Namespaces

XML Namespaces provide a method to avoid element name conflicts.


Name Conflicts

Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.

This XML document carries information in a table:






Apples Bananas

This XML document carries information about a table (a piece of furniture):


African Coffee Table
80
120

If these two XML documents were added together, there would be an element name conflict because both documents contain a

element with different content and definition.


Solving Name Conflicts Using a Prefix

This XML document carries information in a table:



Apples
Bananas

This XML document carries information about a piece of furniture:


African Coffee Table
80
120

Now there will be no name conflict because the two documents use a different name for their

element ( and ).

By using a prefix, we have created two different types of

elements.


Using Namespaces

This XML document carries information in a table:



Apples
Bananas

This XML document carries information about a piece of furniture:


African Coffee Table
80
120

Instead of using only prefixes, we have added an xmlns attribute to the

tag to give the prefix a qualified name associated with a namespace.


The XML Namespace (xmlns) Attribute

The XML namespace attribute is placed in the start tag of an element and has the following syntax:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
Try to go to http://www.onlinexml.blogspot.com


Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.


Default Namespaces

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:

xmlns="namespaceURI"

This XML document carries information in a table:






Apples Bananas

This XML document carries information about a piece of furniture:


African Coffee Table
80
120


Namespaces in Real Use

When you start using XSL, you will soon see namespaces in real use. XSL style sheets are used to transform XML documents into other formats, like HTML.

If you take a close look at the XSL document below, you will see that most of the tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified by the namespace "http://www.w3.org/1999/XSL/Transform":

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



My CD Collection













Title Artist




Mix it