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.
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:
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.
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:
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.
<n:Teacher>Science and humanities</n:Teacher>
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'>
Anantomy Books <html:b> Introduction to the concept</html:b>
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:
Here the namespace prefix is adv and the URI is www.edituse.com/
<?xml version="1.0" encoding="UTF-8"?>
To avoid conflict using prefix as a namespace. Here we have two tables but with different namespace here as info and dt.
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:
<?xml version="1.0" ?>
<Webappxmlns:spider='urn: https://www.w3resource.com/Web content hack'>
Implementation using two namespaces that overrides the default namespace. The following concept illustrates:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<title>Sample Form Example</title>
<load id="frm1" method="get" action="click.asp"/>
<p>The following is XFormsmarkup:</p>
<input ref="frstname" xmlns="http://www.w3.org/2002/xmlform">
<input ref="laname" xmlns="http://www.w3.org/2002/xmlform">
<submit submission="frm1" xmlns="http://www.w3.org/2002/xmlform">
Using Schema Namespace “xs:
<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 ref="Topic Name"/>
<xs:element ref="Programming Languages"/>
<xs:attributeWriterName="Vidhya Tagoor" type="xs:Experience" use="required"/>
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.
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.
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 –