Latest web development tutorials

XML Namespaces

XML Namespaces provide a method 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 naming conflict will occur.

This XML carries HTML table information:

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

This XML 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, since the two documents are included with different content and definition of the <table> element, naming conflicts occur.

XML parser can not determine how to deal with such conflicts.


Use prefixes to avoid naming conflicts

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

This XML carries HTML table and a piece of furniture in a message:

<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 namespace - xmlns attribute

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

Namespacexmlns attribute is defined in the start tag of the element.

Namespace declaration syntax 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, xmlns attribute <table> tag defines h: namespace prefix qualified: and f.

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

Namespace, you can declare or they are used in the element 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 parser will not be used to find information.

Its purpose is to give the namespace a unique name. However, many companies often use the namespace as a pointer to point to the actual existence of a page, this page contains information about the namespace.

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


Uniform resource identifier (URI, stands for Uniform Resource Identifier)

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

The most common URI is theUniform Resource Locator used to identify an Internet domain address (URL).A less common URI is theUniform Resource Name (URN).

In our example, we only use URL.


Default namespace

Element defines the default namespace allows us to omit the prefix in all the sub-elements in the work. Its syntax is as follows:

xmlns="namespaceURI"

This XML carries HTML table information:

<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>


Actual use of the namespace

XSLT is a process for the XML documents into other XML language format, such as HTML.

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

Non-HTML tags have the prefix xsl, and thus identifies 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, please our home page to find XSLT tutorial.