w3reference home
XML Tutorial


Bookmark and Share

XML Namespaces

Sometimes there can be a name conflict when two elements share a same name. It happens normally when two applications that have an element with the same name are merged together. The XML parser won't know that these are separate entities. Hence, a solution to name conflicts is using namespaces. Consider for example the two XML fragments given below:
<book>
<name>XML for dummies</name>
</book>
<author>
<name>
<first_name>Lalit</first_name>
<last_name>Pawar</last_name>
</name>
</author>
When the above two fragments are merged together, there will be a name collision since both of them contain the element "name" that holds different data. To handle this difference, namespaces are used. Therefore element names are prefixed by a label that is unique for a namespace. The namespace is defined by using the attribute "xmlns". It is defined in the start tag of the concerned element. The syntax of the namespace declaration is given below:
xmlns:prefix="URI"
Now, let's have a look at the above syntax. What is a URI? URI stands for Uniform Resource Identifier, a string of characters which identifies an internet resource. The only purpose of using a URI is to have a unique prefix. In fact, the URI may not exist. The parser does not validates it, nor does it look it up for some information. The sole purpose is to give the namespace a unique name.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).


Ways to Specify Namespaces

  • Inside the XML file.
  • Inside the DTD file.

    Inside the XML file

    1) Specify a Namespace for every element

    <catalog>
    <b:book xmlns:b = “http://website/b.html”>
    <b:name xmlns:b = “http://website/b.html”>XML for dummies
    </b:name>
    </b:book>
    <a:author xmlns:a = “http://website/a.html”>
    <a:name xmlns:a = “http://website/a.html”>
    <a:first_name xmlns:a = “http://website/a.html”>Lalit</a:first_name>
    <a:last_name xmlns:a = “http://website/a.html”>Pawar</a:last_name>
    </a:name>
    </a:author>
    </catalog>
    2) Specify all Namespaces within the root element
    <catalog xmlns:b = “http://website/b.html”
    xmlns:a = “http://website/a.html”>
    <b:book>
    <b:name>XML for dummies
    </b:name>
    </b:book>
    <a:author>
    <a:name>
    <a:first_name>Lalit</a:first_name>
    <a:last_name>Pawar</a:last_name>
    </a:name>
    </a:author>
    </catalog>
    The specification given for the that element is also valid for all elements occurring inside it.

    3) Default namespace
    Namespaces do not have to be specified explicitly with prefixes.

    <catalog>
    <book>
    <name xmlns = “http://website/b.html”>XML for dummies
    </name>
    </book>
    <author>
    <name xmlns = “http://website/a.html”>
    <first_name>Lalit</first_name>
    <last_name>Pawar</last_name>
    </name>
    </author>
    </catalog>
    4) Attributes can be explicitly assigned a value and be associated with a namespace.
    <catalog xmlns:b = “http://website/b.html”
    xmlns:a = “http://website/a.html”>
    <b:book b:category="computer">
    <b:name>XML for dummies
    </b:name>
    </b:book>
    <a:author>
    <a:name>
    <a:first_name>Lalit</a:first_name>
    <a:last_name>Pawar</a:last_name>
    </a:name>
    </a:author>
    </catalog>
    If nothing is prefixed with the attribute name, it does not belong to any namespace.

    5) A namespace specification can be overridden.

    <catalog xmlns:b = “http://website/b.html”>
    <b:book>
    <b:name>XML for dummies
    </b:name>
    </b:book>
    <b:author xmlns:b = "http://website/test.html">
    <b:name>
    <b:first_name>Lalit</b:first_name>
    <b:last_name>Pawar</b:last_name>
    </b:name>
    </b:author>
    </catalog>
  • Code Validator
    Learn FTP
    Color finder
    Link Checker
    Free web designs
    Coming soon!
    Interview Questions...
    'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
    Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
    Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.