XML Tutorial

XML Namespace

XML Namespace


Namespace avoid conflicts with element.  For example: In the example below the XML has 2 section one for book author details and other one is having the details of library customers. Both elements have similar tags that might be bight confusing. So to make it more simpler to understand we use namespaces.

<?xml version = "1.0" encoding = "UTF-8" ?>
<library>	
    <author>
      <name>Honey Maxi</name>
      <age>45</age>
      <gender>Male</gender>
    </author>
    <author>
      <name>Jack Mauga</name>
      <age>39</age>
      <gender>Male</gender>
    </author>	
    <customer>
      <name>Zaphod Beeblebrox</name>
      <age>19</age>
      <gender>Male</gender>
    </customer>	
    <customer>
      <name>June Hox</name>
      <age>15</age>
      <gender>Female</gender>
    </customer>		
</library>
Defining a Namespace


Syntax: xmlns:prefix=”URI

xmlns – Namespace declaration always starts with xmlns which stands for XML namespace.
prefix – Then there is a prefix. The prefix can be any name that adds meaning to the element name.
URI – URI is defined to uniquely identify namespace. An XML cannot have 2 same URI, URI should always be unique.

There are multiple places we can define a namespace:

  • Inside a element
    <author xmlns:author="https://www.tutorialsatoz.com/author">
  • Inside a root element
    <library xmlns:customer="https://www.tutorialsatoz.com/customer" xmlns:author="https://www.tutorialsatoz.com/author">
  • Inside XML definition
    <?xml version = "1.0" encoding = "UTF-8" 
      xmlns:customer="https://www.tutorialsatoz.com/customer" 
      xmlns:author="https://www.tutorialsatoz.com/author"
    ?>
Referring Defined Namespace


Once we have defined our namespace we need to refer our elements using the prefix defined.

<?xml version = "1.0" encoding = "UTF-8" 
  xmlns:customer="https://www.tutorialsatoz.com/customer" 
  xmlns:author="https://www.tutorialsatoz.com/author"
?>
<library>	
    <author>
      <author:name>Honey Maxi</author:name>
      <author:age>45</author:age>
      <author:gender>Male</author:gender>
    </author>
    <author>
      <author:name>Jack Mauga</author:name>
      <author:age>39</author:age>
      <author:gender>Male</author:gender>
    </author>	
    <customer>
      <customer:name>Zaphod Beeblebrox</customer:name>
      <customer:age>19</customer:age>
      <customer:gender>Male</customer:gender>
    </customer>	
    <customer>
      <customer:name>June Hox</customer:name>
      <customer:age>15</customer:age>
      <customer:gender>Female</customer:gender>
    </customer>		
</library>
Varun Goel

About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.

Leave a Reply

Your email address will not be published.