xml namespace looks simple, but it is a powerful concept.
Since elements in xml are defined by the developers, it is common that two people in the world might be using same names for tags in two different xml files. If there is a requirement to merge these two files, then tag name conflicts will occur, which is an issue.
The aim of namespace is to avoid conflicts between two xml elements and uniquely identify them.
This is similar to package statement in java. For example, Date class is available in two packages: java.util and java.sql. To identify it uniquely, we need to prefix it fix respective package name.
Namespace is an unique URI(Uniform Resource Identifier)
xmlns attribute:
When a prefix is used for an element in xml, that prefix must be defined before using it.
Syntax: xmlns:prefix="URI"
For ex:
<ad:inputText xmlns:ad="http://suresh.reddy.com/mynamesp1">
<ad:ele2/>
</ad>
namespace prefix is available for all the child elements of an element, for which namespace is added.
Default namespace:
Defining a default namespace allows the developer to avoid prefixing every element in the file.
Default namespace is defined with xmlns attribute, with colon and prefix name.
Syntax: xmlns="URI"
Since elements in xml are defined by the developers, it is common that two people in the world might be using same names for tags in two different xml files. If there is a requirement to merge these two files, then tag name conflicts will occur, which is an issue.
The aim of namespace is to avoid conflicts between two xml elements and uniquely identify them.
This is similar to package statement in java. For example, Date class is available in two packages: java.util and java.sql. To identify it uniquely, we need to prefix it fix respective package name.
Namespace is an unique URI(Uniform Resource Identifier)
xmlns attribute:
When a prefix is used for an element in xml, that prefix must be defined before using it.
Syntax: xmlns:prefix="URI"
For ex:
<ad:inputText xmlns:ad="http://suresh.reddy.com/mynamesp1">
<ad:ele2/>
</ad>
namespace prefix is available for all the child elements of an element, for which namespace is added.
Default namespace:
Defining a default namespace allows the developer to avoid prefixing every element in the file.
Default namespace is defined with xmlns attribute, with colon and prefix name.
Syntax: xmlns="URI"
No comments:
Post a Comment