Learn about namespaces in XML

If you, who have ever worked on XML files, are more or less aware of the namespace concept in XML. Let me give you the following example to give you a first look at it:

Declaring xmlns:c1=”https://huongdanjava.com/class1″ or xmlns:c2=”https://huongdanjava.com/class2″ then the xmlns:c1 or xmlns:c2 are the definitions of the namespace.

Namespace in XML is a tool that allows us to zone and access the value of a certain tag in an XML file easily. Imagine, if you did not have the namespace defined above:

then I have to spend a lot of effort to access information of student named Khanh and address is 789. With namespaces, we can specify, what information we need to get, containing in which namespace (above example: means in which class) and then we can access the value which we want easily. No namespace, you have to access through all tags to check the value that we want to get, and then retrieve the relevant information.

Simply, imagine a namespace like a family. We can have many families whose members have the same name. The family division helps us to identify the member’s information which we want to know, quickly …

The syntax for a namespace in XML is as follows:


  • xmlns stands for XML namespace, this value is invariable.
  • prefix: name identify the namespace.
  • URI: Usually we will get a URI address, such as “https://huongdanjava.com/class2” above. You must ensure that it is unique, not identical to the URIs of other namespaces in our XML file.

In case your XML file has only one namespace as follows:

You just declare the xmlns keyword, leaving the prefix and URI as well. For example:

Add Comment