XML Namespaces
Introduction to XML Namespaces
- XML Namespace is a unique name given to group of elements and attributes to avoid naming collisions.
- URI (Uniform Resource Identifier) is used to assign namespace.
Namespaces Declaration
- The keyword xmlns is used to declare namespace in an XSD document.
- A namespace is declared at the beginning of the document.
Syntax:
<xmlns:prefix=“URI”>
Where,
xmlns is the name of the attribute.
prefix is optional.
URI is the value of the xmlns attribute which is associated with XSD document.
There are two ways of declaring namespaces:
1. Default declaration: In this type of declaration prefix is not used.
Example : Default declaration
<xml version=“1.0 encoding=”UTF-8?>
<schema xmlns="w3.org.com/XMLSchema">
<name>Mayuri</name>
<company>CareerRide</company>
<phone>9800000000</phone>
</information>
In above example, a prefix is not defined with xmlns and URI.
2. Explicit declaration: In this type of declaration, the prefix is defined with xmnls and with URI.
Example : Explicit declaration
<xml version=“1.0 encoding=”UTF-8?>
<schema:information xmlns:info="w3.org.com/XMLSchema">
<info:name>Mayuri</info:name>
<info:company>CareerRide</info:company>
<info:phone>9800000000</info:phone>
</info:information>
In above example prefix info is used with xmlns and URI.