XML Namespaces
Estimated study time: 10 minutes.
As XML documents combine data from multiple sources, it's common for two different vocabularies to use the same element name for different things. Namespaces solve this by qualifying element names so they don't collide.
The Problem Namespaces Solve
<!-- Which "title" is which? -->
<catalog>
<book>
<title>Learning XML</title>
</book>
<course>
<title>Data Analytics</title>
</course>
</catalog>
In this simple case it's fine, but when merging documents from different systems, identical tag names from different vocabularies can clash and cause ambiguity.
Declaring a Namespace
<catalog xmlns:book="http://example.com/book"
xmlns:course="http://example.com/course">
<book:title>Learning XML</book:title>
<course:title>Data Analytics</course:title>
</catalog>
The xmlns:prefix="URI" attribute declares a namespace with a short prefix. The URI doesn't need to point to a real, live webpage — it just needs to be a unique identifier for that vocabulary.
Default Namespace
<catalog xmlns="http://example.com/default">
<title>Applies to this namespace by default</title>
</catalog>
💡 Tip: Namespace URIs are identifiers, not links — don't worry if visiting one in a browser shows nothing. Their only job is to be globally unique.