XML Namespaces
XML namespaces provide a method for avoiding element name conflicts by associating a unique identifier with a set of XML tags.
Problem: Name Conflicts between XML Elements and Resolution
A conflict could happen when you try to mix XML documents from different XML application.
The following is an example of conflict:
<table>
<tr>
<td>Milan</td>
<td>Rome</td>
<td>Venice</td>
</tr>
</table>
<table>
<name>PC desk</name>
<width>90</width>
<length>150</length>
</table>
Both contain a <table> element but the XML elements have different content and meaning. If they merged together, an XML parser has no way to distinguish between the two different meanings of <table>. This is a name conflict.
Solution: Prefixes and Namespace Declarations
The solution is a two-part process:
- Prefix: Add a short prefix to the element names to distinguish them.
- Namespace Declaration: Link that prefix to a unique identifier (a URI).
Step 1: Using Prefixes
We can visually solve the conflict by adding prefixes to our tags. For example:
<firstprefix:table>
<firstprefix:tr>
<firstprefix:td>Milan</firstprefix:td>
<firstprefix:td>Rome</firstprefix:td>
<firstprefix:td>Venice</firstprefix:td>
</firstprefix:tr>
</firstprefix:table>
<secondprefix:table>
<secondprefix:name>PC desk</secondprefix:name>
<secondprefix:width>90</secondprefix:width>
<secondprefix:length>150</secondprefix:length>
</secondprefix:table>
In this way, we have resolved the conflict: elements of the first table have firstprefix: prefix while elements of the second table have secondprefix: prefix.
Basically, we changed the <tagname> to <prefixname:tagname> with prefixes to resolve conflicts
Step 2: Declaring the Namespace with xmlns
A prefix is just a shorthand. To make it official, you must declare it with the xmlns attribute. This declaration binds the prefix to a Namespace URI, which is a unique string that acts as the universal identifier for that set of tags.
Syntax: xmlns:prefix="NamespaceURI"
xmlns: The reserved attribute name for a namespace declaration.prefix: The short prefix you are defining (e.g.,forh).NamespaceURI: A unique string that identifies the namespace. Crucially, this is just an identifier, not a URL to be downloaded. The parser uses this string to distinguish namespaces, but it does not fetch anything from this address.
The namespace is defined using an xmlns attribute in the start tag of an element, following this syntax: xmls:prefixname="URI".
<root>
<firstprefix:table xmlns:firstprefix:tr="http://www.w3.org/TR/html4/">
<firstprefix:tr>
<firstprefix:td>Milan</firstprefix:td>
<firstprefix:td>Rome</firstprefix:td>
<firstprefix:td>Venice</firstprefix:td>
</firstprefix:tr>
</firstprefix:table>
<secondprefix:table xmlns:secondprefix:name="https://tutorialreference.com/furniture">
<secondprefix:name>PC desk</secondprefix:name>
<secondprefix:width>90</secondprefix:width>
<secondprefix:length>150</secondprefix:length>
</secondprefix:table>
</root>
Namespaces can also be declared in the XML root element
<root xmlns:firstprefix="first-URI" xmlns:secondprefix="second-URI">
...
</root>
The purpose of the URI is to give the namespace a unique name. Also, it isn't used by the parser.
XML Namespace as Default Namespace
If one namespace is used much more frequently than others in a document, you can declare it as the default namespace. This allows you to use its tags without any prefix, reducing clutter.
A default namespace is declared by using xmlns without a prefix.
Syntax: xmlns="NamespaceURI"
For example:
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Milan</td>
<td>Rome</td>
<td>Venice</td>
</tr>
</table>
<table xmlns="https://tutorialreference.com/furniture">
<name>PC desk</name>
<width>90</width>
<length>150</length>
</table>
Any unprefixed element in the scope of this declaration is considered part of the default namespace. You can still use prefixes for other namespaces in the same document.
Namespaces and XSLT
XSLT is a language used to transform XML documents into other formats.
Read our XSLT Tutorial to learn more about XSLT.
Conclusion
XML Namespaces are the standard and essential solution for avoiding name conflicts when combining XML from different sources.
- They work by pairing a prefix (like
h:) with a unique Namespace URI. - The declaration is made using the
xmlns:prefix="URI"attribute, typically on the root element. - The Namespace URI is just a unique identifier, not a web address that the parser will visit.
- A default namespace (
xmlns="URI") can be used to make the dominant vocabulary in a document cleaner by removing the need for prefixes.