Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML namespaces

XML namespaces


XML namespaces provide a way to avoid element name conflicts.


Naming Conflicts

In XML, element names are defined by the developer. When two different documents use the same element name, a name conflict occurs.

This XML carries information about an HTML 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 are used together, since both documents contain elements with different content and definitions,

an XML parser cannot determine how to handle such a conflict.


Using prefixes to avoid name conflicts

Name conflicts in XML can be easily avoided by using a name prefix.

This XML carries information about an HTML table and a piece of furniture:

Apples

Bananas

African Coffee Table

80

120

In the example above, there is no conflict, because the two

elements, a name conflict would occur.


XML Namespaces - the xmlns Attribute

When prefixes are used in XML, a so-called namespace for the prefixNamespacingmust be defined.

A namespace is defined in the xmlns attributeof an element's start tag.

The syntax for a namespace declaration is as follows. xmlns:Prefix="URI"。

xmlns:h="http://www.w3.org/TR/html4/"

Apples

Bananas

xmlns:f="http://www.w3cschool.cc/furniture"

African Coffee Table

80

120

In the example above,

elements have different names.

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

A namespace can be declared in the element where it is used or in the XML root element:

xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture"

Apples

Bananas

African Coffee Table

80

120

**Note:** The namespace URI is not used by the parser to look up information.

Its purpose is to give the namespace a unique name. However, many companies often use the namespace as a pointer to an actual web page that contains information about the namespace.

Please visit http://www.w3.org/TR/html4/。


Uniform Resource Identifier (URI)

Uniform Resource Identifier(URI) is a string of characters that identifies an Internet resource.

The most commonly used URI is the one that identifies an Internet domain address,Uniform Resource Locator(URL). Another less commonly used URI isUniform Resource Name (URN)(URN)。

In our examples, we only use URLs.


Default namespaces

Defining a default namespace for an element saves us from using prefixes in all child elements. Its syntax is as follows:

xmlns="

namespaceURI

"

This XML carries information about an HTML table:

The xmlns attribute of the tag defines the qualified namespaces for the h: and f: prefixes.
Apples Bananas

This XML carries information about a piece of furniture:

African Coffee Table

80

120


Namespaces in real use

XSLT is an XML language used to transform XML documents into other formats, such as HTML.

In the XSLT document below, you can see that most of the tags are HTML tags.

Non-HTML tags all have the prefix xsl and are identified by this namespace: xmlns:xsl="http://www.w3.org/1999/XSL/Transform":

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

My CD Collection

Title Artist

If you want to learn about XSLT, please look for theHomeXSLT tutorial on our site.