Introduction:
Namespace in XML provides a unique URI to the XML vocabulary, or a group of related XML constructs. Combination of namespace URI and a local name of an XML construct makes a universal qualified name, also called QName.Namespace and URIs
Namespace URI can be either a concrete URL or an abstract URN. Namespace URI doesn’t need to exist for access over internet. Namespace URI might be just an identifier within the internet domain of namespace owner. There is no central repository on internet allocating or monitoring namespaces URIs used by different XML vocabularies.Namespace Prefix
Many characters which are legal in URI cannot be used in XML names. Hence, solution was devised to create an alias for the URI which would be restricted to legal XML characters. This alias a.k.a prefix is mapped to a namespace URI inside XML document, and serves as proxy for namespace URI.Namespace URI and prefix mapping
Namespace URI to prefix mapping is an attribute based declaration. Declaration syntax is:xmlns[:prefix] ="URI"
Where, xmlns is a reserved keyword. ":prefix" part is optional, and it is defined by user or application generating XML document. Namespace prefix can be composed from any valid XML name character, however prefixes beginning with xml (in any combination of upper or lower case) are reserved. Namespace prefix can be declared in an element at any level within XML document.
Qualified Name (abbr. QName)
QName contains a single colon separating namespace prefix, and local name of an XML construct. Local name of an XML construct cannot contain ‘:’.In following listing of XML document, two prefixes "shipNS", and "billNS" are mapped to two different namespaces.
<order>
<items>
<item>..</item>
..
</items>
<shipNS:shipping xmlns:shipNS="http://www.shipXYZExample.com">
<shipNS:Address>
used by logistic partner
</shipNS:Address>
</shipNS:shipping>
<billNS:billing xmlns:billNS="http://www.billABCExample.com"> >
<billNS:Address>
used for payment processing
</billNS:Address>
</billNS:billing>
</order>
Namespace Scope
Scope of
a namespace declaration applies to an element in which it is declared,
and all the child elements of that element. Namespace declaration can
be overridden in child
element(s) by another similar namespace declaration.In following listing of parentChild document , child_ele overrides the namespace declaration of parent_ele.
<parent_ele xmlns:family="http://www.xyzExample.com">
<child_ele xmlns:family="http://www.pqrExample.com">
</child_ele>
</parent_ele>
Default Namespace
Declaration syntax of a default namespace is:xmlns ="URI"
In the scope of a default namespace, elements with only local names are considered to be qualified and belonging to the URI of default namespace declaration.
In following listing of parentChild document, both parent_ele and child_ele are belonging to a namespace "http://www.xyzExample.com".
<parent_ele xmlns="http://www.xyzExample.com">
<child_ele>
</child_ele>
</parent_ele>
If an element in the scope of default namespace belongs to other namespace or no namespace then it either needs to override the default namespace declaration, or use a QName explicitly.
In following listing of parentChild document, parent_ele belongs to a namespace "http://www.xyzExample.com", however child_ele does not belong to any namespace.
<parent_ele xmlns="http://www.xyzExample.com">
<child_ele xmlns="">
</child_ele>
</parent_ele>
In following listing of parentChild document, parent_ele belongs to a namespace "http://www.xyzExample.com", however child_ele is declared as Qname, where prefix "cNS" is mapped to "http://www.pqrExample.com" namespace.
<parent_ele xmlns="http://www.xyzExample.com">
<cNS:child_ele xmlns:cNS="http://www.pqrExample.com">
</cNS:child_ele>
</parent_ele>
Attributes:
Attributes without prefix doesn’t belong to any namespace, even if an attribute is in the scope of a default namespace. For qualified attributes, attribute name must be written with a namespace prefix, i.e. it must be a Qname.Per XML, start tag of an element should not contain any two attributes with same name, whether both attribute names are qualified or not.
In following listing of goodBad XML document, elements badAttrs1 has two non-qualified attributes with same name, and element badAttrs2 has two qualified attributes with same local name and different prefixes mapped to same namespace. However, element goodAttrs has three attributes out of which two qualified attributes belongs to two different namespace and third attribute is non-qualified, i.e. it doesn't belong to any namespace.
<root xmlns:ns1="urn:example:sun-com" xmlns:ns2="urn:example:sun-com" xmlns:ns3="urn:example:traders-com" >
<badAttrs1 ns1:att="1" att ="2" att="3"/>
<badAttrs2 ns1:att="1" ns2:att ="2" att="3"/>
<goodAttrs ns1:att="1" ns3:att ="2" att="3"/>
...
</root >
XML Parser support for namespace
Both SAX 2.0 and DOM Level 2 parsers supports XML namespaces.SAX 2.0 support namespace through startPrefixMapping(), endPrefixMapping(), startElement(), and endElement() callbacks.
DOM Level 2 supports namespace through additional method to its various DOM Level 1 interfaces.