Updated April 12, 2023
Introduction to XML Namespaces
A namespace can be defined as a simple mechanism by which names are distinguished in the XML document and it is recommended by W3C and comes with a couple of pair local names and namespace. With Namespace, element and attribute name can be assigned to a group. The Namespace is decided by W3C to identify by URI (Uniform Resource Identifiers) references /URL also local names they execute global around the world which sometimes requires global names unique for elements and the main purpose is to avoid the name conflicts as the XML document has same names but with different definitions(different vocabularies has a different meaning). It is mentioned as a specially reserved attribute to be placed in XML Tag. The prefix attribute is given as “xmlns:”, it can be used for names with element and attribute.
Syntax:
Declaration of the namespace in the XML document is very important which are done by making use of a family of reserved attributes. And these attributes can be put on the way directly or default in the following description method:
<element namexmlns: prefix name = “ URI”>
In the syntax, the namespace starts with the keyword ‘prefix’ with (:) and the name is the prefix name, URI is the namespace Identifier. Both local name and namespace mix together to form a universal name. They are defined in a start tag. Declaration of Namespace depends on the families of the attributes which is provided directly or by default. For instance, for prefix =’xmlns:’ Namespace name
The default attribute name is ‘xmlns’
Namespace standard for XHTML is declared below with prefix html and namespace URI:
xmlns:html=http://www.w3.org/1999/xhtml
URI: It provides identifiers that are unique across the globe and this can include characters.
Prefix: A universal name in an XML document is declared using an optional prefix which is been attached to a local name and this is termed as a Qualified name or qname.
Qname::=(prefix ‘:’)
Qualified names examples are XHTML: title, discount: sale, xlink: fit. Here we have xhtml, discount, the link has prefix and title, sale, fit has local part.
How do Namespaces Work in XML?
Namespaces are the difficult section in XML due to elements and attribute names are defined by the developer there occurs a confliction and also, they are termed as vocabularies because it is a collection of definitions and names. The scope of the namespace starts from the start tag and ends with the corresponding end tag, while the empty tag scope is declared by itself. As it is a group of elements, attributes, types in a particular schema. Namespaces require two fundamentals which are stated below:
- It differentiates two similarly named elements to avoid conflictions between names used in different mark-up languages.
The working concept is explained below with different scenarios
Attempt- 1: For a single namespace having more than one prefix. The sample is given below:
<x:studentxmlns:x='urn:oxford:ns'>Grade8</x:student>
<y:studentxmlns:y='urn:oxford:ns'>Grade 10</y:student>
</sch>
Here x, y are two different prefixes with one namespace ‘urn’.
Attempt – 2: The same prefix for two different namespaces m and n are two prefixes with two urn namespace.
<schxmlns:m='urn:Oxford:ns' xmlns:n='urn:osford:ns'>
<m:Teacher>Computer Science</m:Teacher>
<n:Teacher>Science and humanities</n:Teacher>
</sch>
Attempt – 3: Declaring namespace in a root element with no prefix. Root element lib has the namespace.
<lib xmlns='urn:hadvardlibrary:ns' xmlns:html='http://www.w3.org/1999/xhtml'>
<html:body>
Anantomy Books <html:b> Introduction to the concept</html:b>
</html:body>
</lib>
Now, in the next section, we shall see all the above scenario into implementation and results.
Examples to Implement XML Namespaces
Below are the examples of Namespaces in XML:
Example #1
Here the namespace prefix is adv and the URI is www.edituse.com/
Code:
<?xml version="1.0" encoding="UTF-8"?>
<adv:profilexmlns:adv="www.edituse.com/database">
<adv:profilename>Divya Mary</adv:profilename>
<adv:organization>Deloitee Campus</adv:organization>
<adv:contactno>(022) 224-1557</adv:contactno>
</adv:profile>
Output:
Example #2
To avoid conflict using prefix as a namespace. Here we have two tables but with different namespace here as info and dt.
Code:
<parent>
<info:table1 xmlns:info="http://www.w3.org/TR/html4/">
<info:tr>
<info:td>Milk</info:td>
<info:td>Bread</info:td>
</info:tr>
</info:table1>
<dt:table1 xmlns:dt="https://www.EDUCBA.com/FamousPlayers">
<dt:Playername>Krayan Kobe</dt:Playername>
<dt:Place>Toronto</dt:Place>
<dt:Medal>15</dt:Medal>
</dt:table1>
</parent>
Output:
Example #3
Using Default Namespace without using a prefix. Multiple Namespaces can be defined with the same document. All the elements below can use this namespace and these are not applied directly to the attributes. An example is given below:
Code:
<?xml version="1.0" ?>
<Webappxmlns:spider='urn: https://www.w3resource.com/Web content hack'>
<Webname>Web Security</Webname>
<Webname>Web authentication</Webname>
<Webname>Kerberoes</Webname>
</Webapp>
Output:
Example #4
Implementation using two namespaces that overrides the default namespace. The following concept illustrates:
Code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sample Form Example</title>
<blueprint xmlns="http://www.w3.org/2002/xmlform">
<employee>
<candidate xmlns="">
<firstname/>
<lastname/>
</candidate>
</employee>
<load id="frm1" method="get" action="click.asp"/>
</blueprint>
</head>
<body>
<p>The following is XFormsmarkup:</p>
<input ref="frstname" xmlns="http://www.w3.org/2002/xmlform">
<label>First Name</label>
</input>
<br/>
<input ref="laname" xmlns="http://www.w3.org/2002/xmlform">
<label>Last Name</label>
</input>
<br/>
<br/>
<submit submission="frm1" xmlns="http://www.w3.org/2002/xmlform">
<label>click</label>
</submit>
</body>
</html>
Output:
Example #5
Using Schema Namespace “xs:
Code:
<?xml version="1.0"?>
<xs:schematargetNamespace="http://www.educba.com/Artist"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.educba.com/Artist">
<xs:element name="Article" type="xs:string"/>
<xs:element name="Topic Name" type="xs:string"/>
<xs:element name="Programming Languages" type="xs:string"/>
<xs:element name="WriterName">
<xs:complexType>
<xs:sequence>
<xs:element ref="Article"/>
<xs:element ref="Topic Name"/>
<xs:element ref="Programming Languages"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:elementWritername="Article">
<xs:complexType>
<xs:sequence>
<xs:element ref="Name"/>
</xs:sequence>
<xs:attributeWriterName="Vidhya Tagoor" type="xs:Experience" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
The above schema code could be invalid if the namespace “Http://www.educba/Article “ is removed because the name and article element assignment have child elements that reference the elements in the schema. here the complex type element defines a sequence of other followed elements.
Advantages of Namespaces in XML
Below are the advantages of XML Namespaces:
- It avoids duplicate element names and attributes defined in an XML document where the XML element defined in one organization product may conflict with the other. The main reason for confliction is when we use the XML document in the different application processes.
- With namespace elements and attributes can be reused in other documents or schema without any error.
- it enhances readability and the declaration of them is placed anywhere within an XML document. Though Namespace is a very controversial part in XML still it is used as an approach in XML information architecture to solve a different problem.
- Another advantage is reusability has it has individual software application to do so, reusing the elements. It also has good modularity and extensibility as well.
Conclusion
Therefore, this article explains clearly why namespaces are needed in XML Document also we can use own namespaces in our XML document. The most primary concept in using namespace is to learn how to keep these principles straight. Anybody can create an XML namespace by describing element type and attributes. The only concepts which don’t have namespace are CSS, which is expected to propose earlier.
Recommended Articles
This is a guide to XML Namespaces. Here we discuss the Introduction to XML Namespaces and its different advantages along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –