XML – Namespaces

XML namespaces are generally used for naming the elements and attributes uniquely. They are defined in W3C recommendation.

In XML, the data enclosed with in elements and those elements naming sometimes may match with reserved words like body, head in XHTML. So in order to get rid of such situations and providing unique identification of all elements mentioning in XML needs XML namespaces.

Syntax:

<name:element xmlns:name='URI'>  
<html:html xmlns:html="http://www.w3.org/TR/xhtml2/"> 

We can observe from the below example, showing that two different elements trying to use the same attribute. Here come the XML namespaces to resolve this situation.

Usage requirement:

<?xml version="1.0" encoding="UTF-8"?> 
<html> 
<head> 
	<title> xml learning</title> 
	<script type="text/javascript" src="/xmlway.js"> </script> 
</head> 
<head> 
	<eyes>brown</eyes> 
	<hair>black</hair> 
</head> 
</html> 

So, the XMl namespace should reserve the xml attribute we place in the tag and it looks like a prefix along with URI as you seen in the above syntax. URI means Uniform Resource Identifier or Uniform Resource Locator which is nothing but a domain address that usually identify in our browser.

Using xmlns for the requirement:

<?xml version="1.0" encoding="UTF-8"?> 
<html:html xmlns:html="http://www.w3.org/TR/xhtml2/"> 

<html:head> 
	<html:title> xml learning</html:title> 
	<html:script type="text/javascript" src="/xmlway.js"> </html:script> 
</html:head> 

<bodypart:head xmlns:bodypart="http://www.wateverURLitis.com/bodypart"> 
<bodypart:eyes> 6ft</bodypart:eyes> 
<bodypart::hair> 155 lbs</bodypart:hair> 
</bodypart:head> 

</html:html>