XML Namespaces - XML

The biggest advantage of using XML is the ability to create custom tags. You can create a vocabulary of tags that can be applied to an application or similar applications. Consider the Student.xml file that you created in the previous section. The file contains tags, such as Student, Name, Address, State, and Zip, which are used to describe the information about the students who are studying in Form 5.

Because XML allows you to declare user-defined tags, it is likely that another user will create a tag with the same name and use it in a different context. Consider a situation in which you create a tag with the name <Average> to store the average scores of students who are studying in Form 5. However, another user can create a tag <Average> to store the average number of students who enroll for a course in a month. The following examples show use of the <Average> tag in different contexts:

Now, consider the following code that uses the <Average> tag in a different context:

As you can see, use of the tag is different. This situation can lead to a problem if you try to integrate data from these two documents.

The World Wide Web Consortium (W3C), the group that issues XML specifications, found a solution to this problem in the form of namespaces. An XML namespace is a collection of names of elements or attributes. This collection of names can be referred by a Uniform Resource Identifier (URI). Therefore, you can include a namespace in an XML document to uniquely identify the elements and attributes in the XML document.

Element and attribute names are stored in a structured format in a namespace. A DTD can be considered an example of a namespace that is referred to by its URL.

Before using a namespace, you need to declare it. The following section discusses declaring Namespace

Declaring XML Namespaces

You declare XML namespaces by using the xmlns keyword. A namespace is referred to by its URI. Therefore, while you are declaring a namespace, you also need to mention the URI that you use to access the namespace. The syntax for declaring a namespace is as follows:


As you can see, xmlns is an attribute that takes a value as the URI of the namespace. After you have created a namespace, you can create elements with the same name in different namespaces. For example, you can declare two namespaces and have the <Average> tag in both these namespaces. Consider the following code:

To uniquely identify an element, you need to prefix it with the name of the namespace. However, you can not prefix an element with the URI of the namespace, therefore, while declaring a namespace, you can assign an alias name to the namespace. This alias name is called a namespace prefix, or simply prefix. You then need to prefix the alias name with an element name to uniquely identify it. The syntax for declaring a namespace with a prefix is as shown:


You can provide an alias name as follows:

xmlns:student="http://www.student.com/studentdetails/student.dtd" xmlns:course="http://www.student.com/coursedetails/course.dtd"

The syntax to access an element in a namespace is this:


Now, the <Average> tag in the student namespace can be accessed as follows:


Using XML Namespaces

After declaring a namespace, you can use it in an XML document. The following code declares a namespace and uses it in the previous example:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics