Advanced Web Technologies


Namespaces


Objectives

The objective of this unit is the reader to define Namespaces and understand the concept of prefixed and default Namespaces. The reader should able to use Namespaces in XML documents.


2.1 Introduction

An XML namespace is a collection of names that are identified by a Uniform Resource Identifier (URI) reference, which are used in XML documents as element types and attribute names. They are defined in a W3C recommendation.

The purpose of namespaces is to differentiate elements and attributes from different vocabularies with different meanings and that happen when they have the same name and to group all the related elements and attributes from a single XML application.

Here in following XML document, the names are conflicted.

      <?xml version="1.0" encoding="ISO-8859-15"?>

      <html>

          <body>

               <p>Welcome to my Health Resource</p>

         </body>

        <body>

               <height>6ft</height>

               <weight>155 lbs</weight>

        </body>

      </html>

We have two very different elements that want to use the same name: body. The solution to this problem is to create XML Namespaces to differentiate between these two similarly named elements.

2.2 XML Namespace Declarations

For declaring namespace in XML prefixed namespace or default namespace are used.

2.2.1 Prefixed namespace Declaration

A prefixed namespace must be declared before it is used. The declaration has the following syntax:

                             xmlns:prefix_name="URI"

This declaration informs the processing software that any element that is prefixed by the prefix_name followed by the colon is in the namespace of the URI.

URI is an abbreviation of Uniform Resource Identifier. URIs are a well-known system for creating unique identifiers.

Examples:

1. 

       <?xml version="1.0" encoding="ISO-8859-15"?>

       <html:html xmlns:html='http://www.w3.org/TR/xhtml1/'>

          <html:body>

              <html:p> Welcome to my Health Resource </html:p>

          </html:body>

          <health:body xmlns:health='http://www.example.org/health'>

               <health:height> 6ft </ health:height>

               <health:weight> 155 lbs </ health:weight>

          </health:body>

       </html:html>

2. 

       <dataroot xmlns:html="http://www.w3.org/TR/html4/ xmlns:furniture ="http://www.panemirates.com/living">

         <html:table">

            <html:tr>

               <html:td>Cars</html:td>

               <html:td>Buses</html:td>

            </html:tr>

          </html:table>

          <furniture:table>

                < furniture:name>Dining Table</ furniture:name>

                < furniture:width>80</ furniture:width>

                < furniture:length>120</ furniture:length>

          </ furniture:table>

      </dataroot>

Whether you choose to declare a namespace on the root element or on some element further down the hierarchy is mostly a matter of personal preference and convenience in the document at hand. Some developers prefer to declare all namespaces on the root element. Others prefer to declare the namespaces closer to where they’re actually used.

2.2.2 Default namespaces Declaration

Syntax to  declare default namespace is

          xmlns="URI"

The element in which default namespace is declared and, its child elements are considered to be in the defined default namespace unless otherwise they have an explicit prefix mentioned.

Attributes are never subject to the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace.

Example:

    <html xmlns="http://www.w3.org/HTML/1998/html4"  xmlns:scopus="http://www.scopus.com/journals">

         <head><title>Journal Review</title></head>

         <body>

             <scopus:jreview>

                   <scopus:title>Big Data Analysis</scopus:title>

                   <table>

                       <th><td>Author</td> <td>Area of Research</td><td>Submitted Date</td></th>

                       <tr scopus:align="left">

                           <td><scopus:author>Abdul Rahiman Shaik</scopus:author></td>

                           <td><scopus:area_of_research> Deep Learning </scopus:area_of_research></td>

                           <td><scopus:submitted_date>16/10/2018</scopus: submitted _date></td>

                       </tr>

                   </table>

             </scopus:jreview >

         </body>

     </html>


Unit Summary

Namespaces are structured as hierarchies to allow reuse of names in different contexts. Namespaces used to group all the related elements and attributes from a single XML document. Different specifications have taken different approaches on how namespace information is presented to applications.


References

1. https://en.wikipedia.org/wiki/XML_namespace

2. https://en.wikipedia.org/wiki/Namespace

Return to top