XML namespace


May 27, 2021 19:00 XML


Table of contents


XML namespace

This section explains the XML namespace, which is a unique set of names that are determined by the URI (unified resource identifier).

XML namespaces provide a way to avoid element naming conflicts.


Naming conflict

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

This XML carries information about HTML tables:

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

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

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

If the two XML documents are used together, a naming conflict occurs because both documents contain elements with different content and definitions.

The XML parser cannot determine how to handle such conflicts.


Use the prefix to avoid naming conflicts

Naming conflicts in XML can be easily avoided by using the name prefix.

The 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 elements have different names.


XML namespace - xmlns property

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

The namespace is defined in the xmlns property of the element's start label.

The syntax of the namespace declaration is as follows. xmlns: Prefix s "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="//www.w3cschool.cn/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 property of the label defines the qualified namespace for the h: and f: prefixes.

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

Namespaces, which can be declared in the element they are using or in the XML root element:

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="//www.w3cschool.cn/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 parsers to find information.

The goal is to give the namespace a unique name. However, many companies often use namespaces as pointers to actual Web pages that contain information about namespaces.

Visit http://www.w3.org/TR/html4/.


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

A Unified Resource Identifier (URI) is a string of characters that can identify An Internet resource.

The most commonly used URI is the Unified Resource Locator (URL) that identifies the Internet domain name address. Another less commonly used URI is Unified Resource Naming (URN).

In our case, we only use URLs.


The default namespace

Defining a default namespace for an element allows us to save the work of using prefixes in all child elements. Its syntax is as follows:

xmlns="namespaceURI" 

This XML carries information about HTML tables:

<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="//www.w3cschool.cn/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table> 


The namespace in actual use

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

As you can see in the XSLT documentation below, most of the labels are HTML tags.

Non-HTML labels have a prefix xsl, which is identified by the 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, find the XSLT tutorial on our home page.