XML namespace
XML namespaces provide a way to avoid element naming conflicts.
Naming conflict
In XML, element names are defined by developers. When two different documents use the same element name, a naming conflict occurs.
This XML carries the information of the HTML table:
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML document carries information about a table (a piece of furniture):
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these two XML documents are used together, since both documents contain < with different content and definitions ;table> element, a naming conflict will occur.
The XML parser cannot determine how to handle such conflicts.
Use prefixes to avoid naming conflicts
Naming 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: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 above example, there will be no conflict because the two <table> elements have different names.
XML Namespace - xmlns attribute
When using prefixes in XML, a so-called namespace for the prefix must be defined.
The namespace is defined in the xmlns attribute of the element's opening tag.
The syntax of namespace declaration is as follows. xmlns:Prefix="URI".
<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 above example, the xmlns attribute of the <table> tag defines the qualified namespace for the h: and f: prefixes.
When a namespace is defined in the opening tag of an element, all child elements with the same prefix will be associated with the same namespace.
Namespaces can be declared in the element in which they are used or in the XML root element:
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: Namespace URIs are not used by the parser to find information.
The purpose is to give the namespace a unique name. However, many companies often use namespaces as pointers to actual existing web pages that contain information about the namespace.
Please visit http://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 Uniform Resource Locator (URL) used to identify an Internet domain name address. Another less commonly used URI is Uniform Resource Naming (URN).
In our example, we only use URLs.
Default namespace
Defining a default namespace for an element saves us the work of using prefixes in all child elements. Its syntax is as follows:
This XML carries the information of the HTML table:
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
This XML carries information about a piece of furniture:
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
Namespaces in actual use
XSLT is an XML language used to convert 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 are prefixed with xsl and identified by this namespace: xmlns:xsl="http://www.w3.org/1999/XSL/Transform":
<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, find XSLT tutorials on our homepage.