When XML standard was established, different XML languages (called also XML grammars) were created. These grammars are specific to certain industries, processes, and information types. In many cases it is important to extend one type of XML markup with additional business-specific elements, or to create XML documents that combine several different grammars.    XML Namespaces is a standard which solves this problem. The core idea behind this standard is that every XML markup language has its own namespace that uniquely identifies all related elements. Technically, XML namespaces disambiguate elements by making it clear to which markup language they belong. All XML namespaces use universal resource identifiers (URIs) which look like a webpage URL. For example,  https://www.mycompany.com/myxmlstandard is a typical name for a namespace. URIs are used for XML namespaces because they are more likely to be unique and usually when you create a new XML language, you will use a URI that points to a website or domain controlled by you. That way, you can be sure that no one else will use that URI. Sometimes Uniform resource names (URNs) are used to prevent confusion with website addresses. URNs start with the prefix urn: and can incorporate a domain name or unique identifier (such as a GUID). One example is urn:schemas-microsoft-com.

You can specify that an element belongs to a specific namespace, by adding the xmlns attribute to the start tag and indicate the namespace. For example For example, the <Title> element shown here is part of the https://www.Books.com/BooksList namespace

<Title xmlns=”https://www.Books.com/BooksList”>

Harry Potter and the Deathly Hallows

</Title>

You can assign a namespace as default namespace for all child elements in the fashion as shown next.

 

<?xml version=”1.0″?>

<BooksList

xmlns=”https://www.Books.com/BooksList”>

<Book>

<ISBN-13> 978-0545139700</ISBN-13>

<Title>Harry Potter and the Deathly Hallows</Title>

<Author>J.K. Rowling</Author>

<Price>14.99</Price>

<Available>True</Available>

</Book>

<!– Other books omitted. –>

</BooksList>

 

In this way for you will not be necessary to type the full namespace URI every time you write an element in an XML document.

In complex documents, you have markup from more than one XML language, and you need to place different sections into different namespaces. In this case, you can use namespace prefixes to sort out the different namespaces.

Namespace prefixes are short character sequences that you can insert in front of a tag name to indicate its namespace. You define the prefix in the xmlns attribute by inserting a colon (:) followed by the characters you want to use for the prefix. Here’s the BooksList document rewritten to use the prefix new.

 

<?xml version=”1.0″?>

<new:BooksList

xmlns:new=”https://www.Books.com/BooksList”>

<new:Book>

< new:ISBN-13> 978-0545139700</new:ISBN-13>

< new:Title>Harry Potter and the Deathly Hallows</new:Title>

< new:Author>J.K. Rowling</new:Author>

< new:Price>14.99</new:Price>

< new:Available>True</new:Available>

</new:Book>

<!– Other books omitted. –>

</new:BooksList>

 

Namespace prefixes are simply used to map an element to a namespace. The actual prefix you use isn’t important as long as it remains consistent throughout the document. By convention, the attributes that define XML namespace prefixes are usually added to the root element of an XML document. Although the xmlns attribute looks like an ordinary XML attribute, it isn’t. The XML parser interprets it as a namespace declaration.