XML Namespaces


XML Namespaces provide a way to avoid element name conflicts.


Name Conflicts

In XML, element names are defined by developers. When two different documents use the same element name, name conflicts occur.

This XML carries information about an HTML table:

<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

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

<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

If these two XML documents are used together, name conflicts will occur because both documents contain <table> elements with different content and definitions.

XML parsers cannot determine how to handle this type of conflict.


Using Prefixes to Avoid Name Conflicts

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

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

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

In the example above, there is no conflict because the two <table> elements have different names.


XML Namespaces - The xmlns Attribute

When using prefixes in XML, a so-callednamespacefor the prefix must be defined.

The namespace is defined in thexmlns attributeof the element's start tag.

The syntax for declaring a namespace is as follows. xmlns:prefix="URI"。

<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table xmlns:f="http://www.w3cschool.cc/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>

In the example above, the xmlns attribute of the <table> tag defines the qualified namespace for the h: and f: prefixes.

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

Namespaces can be declared in the elements where they are used or in the XML root element:

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

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>

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 visithttp://www.w3.org/TR/html4/。


Uniform Resource Identifier (URI, full name: Uniform Resource Identifier)

Uniform Resource Identifier(URI) is a string of characters that can identify Internet resources.

The most commonly used URI is the one used to identify Internet domain name addresses, theUniform Resource Locator(URL). Another less commonly used URI is theUniform Resource Name(URN)。

In our examples, we only use URLs.


Default Namespaces

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

xmlns="namespaceURI"

This XML carries information about an HTML table:

<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML carries information about a piece of furniture:

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>


Namespaces in Practice

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

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

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

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

If you want to learn about XSLT, please visit ourhomepageto find the XSLT tutorial.


Other Extensions