In-Depth Guide to XML Namespacing
What is XML Namespace?
An XML Namespace is a way to avoid element name conflicts when combining multiple XML vocabularies. It
provides uniquely named elements and attributes.
Why XML Needs Namespaces?
XML tags can be reused for different data. When combining documents with similar tags, conflicts arise.
Namespaces differentiate these tags by associating them with a unique URI.
Syntax of XML Namespaces
Syntax: xmlns:prefix="URI"
- xmlns: declares the namespace
- prefix: a short name used to qualify elements
- URI: a unique identifier, often a URL (not required to exist)
Example with Prefixes
<root xmlns:bk="http://example.com/books" xmlns:mg="http://example.com/magazines">
<bk:title>Java Programming</bk:title>
<mg:title>Tech Monthly</mg:title>
</root>
Default Namespace
Instead of using a prefix, you can define a default namespace that applies to child elements:
<book xmlns="http://example.com/books">
<title>XML Guide</title>
</book>
In-Depth Guide to XML Namespacing
Mixing Namespaces
Multiple namespaces can be used together:
<doc xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">
<h:table>...</h:table>
<f:table>...</f:table>
</doc>
Attribute Namespaces
Attributes can be namespaced too:
<product xmlns:ns="http://example.com/ns" ns:category="electronics">
<name>Smartphone</name>
</product>
Real-World Use Cases
- XHTML: <html xmlns="http://www.w3.org/1999/xhtml">
- SVG: <svg xmlns="http://www.w3.org/2000/svg">
- SOAP: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
Best Practices
- Use meaningful prefixes
- Don't assume URI needs to point to a live resource
- Use prefixes over default namespace when combining vocabularies
Summary Table
Feature | Description
In-Depth Guide to XML Namespacing
---------------------|----------------------
xmlns | Declares a namespace
xmlns:prefix="URI" | Declares with prefix
default namespace | Applies without prefix
URI | Logical identifier
prefix:tagname | Qualified name