Chapter 8: XML Namespaces
XML Namespaces — written as if we are sitting together, I’m drawing on the whiteboard, showing examples step by step, and explaining why this concept exists, why it is important, and how people actually use it in real projects.
Let’s go slowly and clearly — this is one of the most confusing parts of XML when you first meet it, but once you understand the problem it solves, it becomes very logical.
1. The Problem That Namespaces Solve
Imagine you are combining data from two different companies or two different systems.
Company A uses this XML:
|
0 1 2 3 4 5 6 7 8 9 |
<book> <title>Atomic Habits</title> <author>James Clear</author> </book> |
Company B also uses the word book, but in a completely different meaning:
|
0 1 2 3 4 5 6 7 8 9 10 |
<book> <title>Booking Confirmation #784512</title> <date>2025-07-03</date> <customer>Rahul Mehta</customer> </book> |
Now imagine you want to put both inside one document:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- What happens now? --> <library> <book> <title>Atomic Habits</title> <author>James Clear</author> </book> <book> <title>Booking Confirmation #784512</title> <date>2025-07-03</date> </book> </library> |
Problem: Which <book> is which? Which <title> belongs to the book (novel) and which belongs to the booking?
→ Name collision — the tags have the same name but different meanings.
XML Namespaces solve exactly this problem.
They let you say:
“This <book> comes from Company A’s vocabulary” “This <book> comes from Company B’s vocabulary”
It’s like putting a family name or company logo in front of each tag so we know which “language” it belongs to.
2. How Namespaces Actually Look (First Real Example)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="UTF-8"?> <library xmlns="http://example.com/books" xmlns:bk="http://example.com/books" xmlns:ord="http://example.com/orders"> <!-- Default namespace (no prefix) --> <book> <title>Atomic Habits</title> <author>James Clear</author> </book> <!-- Order namespace (with prefix ord:) --> <ord:book> <ord:title>Booking #784512</ord:title> <ord:customer>Rahul Mehta</ord:customer> </ord:book> </library> |
What changed?
- xmlns=”http://example.com/books” → default namespace → any tag without prefix belongs to this namespace
- xmlns:ord=”http://example.com/orders” → prefix ord: → tags that start with ord: belong to this namespace
Now:
- <book> and <title> → mean book as in novel
- <ord:book> and <ord:title> → mean book as in reservation
No more confusion!
3. The Three Most Important Namespace Concepts
| Concept | What it really is | Example from above |
|---|---|---|
| Namespace URI | A unique string (usually URL-like) that identifies the vocabulary | http://example.com/books |
| Prefix | Short name you choose (usually 2–5 letters) | ord:, bk:, xsl:, soap:, xs: |
| Default namespace | Namespace that applies to tags with no prefix | xmlns=”http://example.com/books” |
Very important rules:
- The URI does not need to point to a real webpage (it’s just an identifier)
- You can choose any prefix you want (a:, my:, book: …)
- The prefix is local — it only exists inside this document
4. Real-World Examples You Will Actually See
Example 1 – SOAP Web Service (very classic)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header>...</soap:Header> <soap:Body> <ns:getWeather xmlns:ns="http://weather.example.com"> <ns:city>Hyderabad</ns:city> </ns:getWeather> </soap:Body> </soap:Envelope> |
Here:
- soap: → SOAP vocabulary
- ns: → custom service vocabulary
Example 2 – Indian GST e-Invoice (real format used in India)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<Invoice xmlns="urn:un:unece:uncefact:data:standard:Invoice:100" xmlns:cac="urn:un:unece:uncefact:data:standard:CommonAggregateComponents:100" xmlns:cbc="urn:un:unece:uncefact:data:standard:CommonBasicComponents:100"> <cbc:ID>INV/2025/0789</cbc:ID> <cbc:IssueDate>2025-07-03</cbc:IssueDate> <cac:AccountingSupplierParty> <cac:Party> <cbc:Name>TechTrend Innovations</cbc:Name> <cbc:CompanyID>36AAECT4567P1Z2</cbc:CompanyID> </cac:Party> </cac:AccountingSupplierParty> ... </Invoice> |
This is UBL – Universal Business Language — one of the most widely used namespace-heavy formats.
- cbc: → basic components (ID, Name, Date…)
- cac: → aggregate components (Party, Address…)
- Default namespace → main Invoice structure
Example 3 – Android Manifest (very common)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:label="@string/app_name" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> |
Almost everything uses android: prefix.
5. Common Ways to Declare Namespaces
| Style | Syntax example | When people use it |
|---|---|---|
| Default namespace + prefixes | xmlns=”…” + xmlns:pre=”…” | Most common style |
| Only prefixes (no default) | xmlns:book=”…” xmlns:ord=”…” | When mixing many vocabularies |
| Default namespace only | xmlns=”http://example.com/data” | Simple documents with one vocabulary |
| On child elements | <root><data xmlns:ns=”…”>…</data></root> | When namespace starts deeper in the document |
6. Quick Summary – The Most Important Things to Remember
- Namespaces prevent name collisions when combining different vocabularies
- A namespace is identified by a URI (usually looks like a URL)
- You give it a short prefix (soap:, ns:, cac:, android:…)
- Default namespace applies to tags with no prefix
- Prefixes are local to the document — you can choose any name
- Real formats (SOAP, UBL, Android, XHTML, SVG, Office XML…) heavily use namespaces
Think of it like this:
Without namespaces: everyone shouting the same words → confusion With namespaces: everyone wearing a different colored shirt → “blue team book” vs “red team book”
Would you like to continue with one of these next?
- How to read / write namespace-aware XML in code (Java, Python, JavaScript…)
- Why some people hate namespaces and when they are overused
- Real example: building a document that uses multiple namespaces at once
- Difference between namespace URI and schema location
- Common tools to visualize or validate namespace usage
- Namespaces in XPath, XSLT, XSD
Just tell me which direction feels most useful or interesting right now! 😊
