Javatpoint Logo
Javatpoint Logo

XML Namespaces

XML Namespace is used to avoid element name conflict in XML document.

XML Namespace Declaration

An XML namespace is declared using the reserved XML attribute. This attribute name must be started with "xmlns".

Let's see the XML namespace syntax:

Here, namespace starts with keyword "xmlns". The word name is a namespace prefix. The URL is a namespace identifier.

Let's see the example of XML file.

Test it Now

Namespace Prefix: cont

Namespace Identifier: http://sssit.org/contact-us

It specifies that the element name and attribute names with cont prefix belongs to http://sssit.org/contact-us name space.

In XML, elements name are defined by the developer so there is a chance to conflict in name of the elements. To avoid these types of confliction we use XML Namespaces. We can say that XML Namespaces provide a method to avoid element name conflict.

Generally these conflict occurs when we try to mix XML documents from different XML application.

Let's take an example with two tables:

Table1:

Table2: This table carries information about a computer table.

If you add these both XML fragments together, there would be a name conflict because both have <table< element. Although they have different name and meaning.


How to get rid of name conflict?

1) By Using a Prefix

You can easily avoid the XML namespace by using a name prefix.

Note: In this example, you will get no conflict because both the tables have specific names.

2) By Using xmlns Attribute

You can use xmlns attribute to define namespace with the following syntax:

Let's see the example:

In the above example, the <table> element defines a namespace and when a namespace is defined for an element, the child elements with the same prefixes are associated with the same namespace.

Note: The Namespace URI used in the above example is not necessary at all. It is not used by parser to look up information. It is only used to provide a unique name to the Namespace identifier.

Uniform Resource Identifier (URI)

Uniform Resource Identifier is used to identify the internet resource. It is a string of characters.

The most common URI is URL (Uniform Resource Locator) which identifies an internet domain address.

There is also an URI name URN (Universal Resource Name) but it is not so common. We have used only URL's in all our examples.


The Default Namespace

The default namespace is used in the XML document to save you from using prefixes in all the child elements.

The only difference between default namespace and a simple namespace is that: There is no need to use a prefix in default namespace.

You can also use multiple namespaces within the same document just define a namespace against a child node.

Example of Default Namespace:

You can see that prefix is not used in this example, so it is a default namespace.

Note: If you define a namespace without a prefix, all descendant elements are considered to belong to that namespace.

Next TopicDTD vs XSD




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA