XML Namespaces

To avoid element name conflicts, a method is facilitated by XML Namespaces.

Name Conflicts:

Defined by the developer, the element names in XML certainly results in a conflict while mixing XML documents from different XML applications.

Example 1:

<table>
  <tr>
    <td>Name</td>
    <td>Department</td>
  </tr>
</table>

Explanation:

In the above example, the XML carries the HTML table information.

Example 2:

<table>
  <name>Side Table</name>
  <width>26</width>
  <length>22</length>
</table>

Explanation:

In the above example, the XML carries the information about a table i.e., a piece of furniture.

There would surely be a name conflict if the above two XML fragments were added together. The reason is simple, the <table> element is present in both the fragments, but with different content and meaning. To handle these differences, the user or an XML application may no be capable.

Solving the Name Conflict Using a Prefix:

A name prefix is used to avoid name conflicts in XML.

Example:

<h:table>
  <h:tr>
    <h:td>Name</h:td>
    <h:td>Department</h:td>
  </h:tr>
</h:table>
 
<f:table>
 <f:name>Side Table</f:name>
  <f:width>26</f:width>
  <f:length>22</f:length>
</f:table>

Explanation:

In the above example, the XML carries information about an HTML table and a piece of furniture, but with no conflict because the two <table> elements have different names.

XML Namespaces – The xmlns Attribute:

A namespace for the prefix must be defined while using prefixes in XML. The xmlns attribute is used in the start tag of an element, to define a namespace. Defining a namespace for an element means that all the child elements with the same prefix are associated with the same namespace.

Syntax: Namespace declaration:

xmlns:prefix="URI"

Example:

<root>
<h:table xmlns:h="http://www.w3spoint.com/TR/html4/">
  <h:tr>
    <h:td>Name</h:td>
    <h:td>Department</h:td>
  </h:tr>
</h:table>
 
<f:table xmlns:f="https://www.w3spoint.com/furniture">
 <f:name>Side Table</f:name>
  <f:width>26</f:width>
  <f:length>22</f:length>
</f:table>
</root>

Explanation:

In the above example, the first <table> element xmlns attribute provides a qualified namespace to the h: prefix, while the second <table> element xmlns attribute provides a qualified namespace to the f: prefix.

Example: Declaring Namespaces in the XML root element:

<root xmlns:h="http://www.w3spoint.com/TR/html4/"
xmlns:f="https://www.w3spoint.com/furniture">
 
<h:table>
  <h:tr>
    <h:td>Name</h:td>
    <h:td>Department</h:td>
  </h:tr>
</h:table>
 
<f:table>
 <f:name>Side Table</f:name>
  <f:width>26</f:width>
  <f:length>22</f:length>
</f:table>
 
</root>

Uniform Resource Identifier (URI):

A string of characters that is used to identify an Internet Resource, is called a Uniform Resource Identifier (URI). The Uniform Resource Locator (URL) is the most common URI. It identifies an Internet domain address. The Uniform Resource Name (URN) is a not so common type of URI.

To check the information, the namespace URI is not used by the parser and the main purpose of using a URI is to give the namespace a unique name. The namespace is, however, often used as a pointer to a web page containing namespace information.

Default Namespaces:

To save us from using prefixes in all the child elements, we can define a default namespace for an element.

Syntax:

xmlns="namespaceURI"

Example 1:

<table xmlns="http://www.w3spoint.com/TR/html4/">
  <tr>
    <td>Name</td>
    <td>Department</td>
  </tr>
</table>

Explanation:

In the above example, the XML carries the HTML table information.

Example 2:

<table xmlns="https://www.w3spoint.com/furniture">
  <name>Side Table</name>
  <width>26</width>
  <length>22</length>
</table>

Explanation:

In the above example, the XML carries the information about a table i.e., a piece of furniture.

Namespaces in Real Use:

To transform XML documents into other formats, the XSLT language is used.

Example:

<?xml version="1.0" encoding="UTF-8"?>
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="/">
<html>
<body>
  <h2>My Music Collection</h2>
  <table border="1">
    <tr>
      <th style="text-align:left">Song</th>
      <th style="text-align:left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/music">
    <tr>
      <td><xsl:value-of select="song"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
 
</xsl:stylesheet>

Explanation:

In the above example, the XML document is used to transform XML into HTML. To identify the XSLT elements inside an HTML document, the namespace “http://www.w3.org/1999/XSL/Transform” is used.

Please Share