XML Tutorial menu

XML Namespaces

Namespaces stop name clashes when one XML document mixes elements from different sources.

The Problem

Because you choose your own tag names, two people can use the same name for different things. A furniture shop's <table> is not the same as an HTML <table>:

Example

xml
<table>
  <tr><td>Chair</td><td>Rs 3500</td></tr>
</table>

Example

xml
<table>
  <material>Sheesham wood</material>
  <seats>6</seats>
</table>

If both appear in one document, a program cannot tell them apart.

Prefixes

A prefix in front of the name solves it:

Example

xml
<h:table>
  <h:tr><h:td>Chair</h:td></h:tr>
</h:table>

<f:table>
  <f:material>Sheesham wood</f:material>
</f:table>

But prefixes alone are not enough: someone else might also choose h. So each prefix is linked to a unique name.

The xmlns Attribute

You declare a namespace with xmlns:prefix="URI":

Example

xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog
  xmlns:h="http://www.w3.org/1999/xhtml"
  xmlns:f="https://example.com/furniture">

  <h:table>
    <h:tr><h:td>Price list</h:td></h:tr>
  </h:table>

  <f:table>
    <f:material>Sheesham wood</f:material>
    <f:seats>6</f:seats>
  </f:table>
</catalog>

The URI (like https://example.com/furniture) is just a unique name. Programs do not open it, and it does not need to be a real web page. Companies use their own domain so that nobody else uses the same URI.

What really counts is the URI, not the prefix. f:table and x:table are the same element if both prefixes point to the same URI.

Default Namespace

If you write xmlns="..." with no prefix, every element without a prefix belongs to that namespace. This keeps files clean:

Example

xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2026-10-15</lastmod>
  </url>
</urlset>

This is a real website sitemap. urlset, url, loc and lastmod all belong to the sitemap namespace.

Namespaces in Files You Know

FileNamespace URI
SVG imagehttp://www.w3.org/2000/svg
Sitemaphttp://www.sitemaps.org/schemas/sitemap/0.9
XML Schemahttp://www.w3.org/2001/XMLSchema
Android layouthttp://schemas.android.com/apk/res/android

For example, an SVG file starts like this:

Example

xml
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="orange"/>
</svg>
Output
An orange circle, 80 pixels wide, in the middle of a 100 by 100 image.

Without the xmlns, a browser opening the .svg file would not know these are SVG drawing elements.

Scope

A namespace declaration applies to the element where it is written and to everything inside it. You can declare namespaces on the root (most common) or on any inner element.

Attributes and Namespaces

Attributes without a prefix do not belong to the default namespace; they simply belong to their element. Attributes with a prefix, like android:text="Hello", belong to that prefix's namespace.

Exercise

Exercise

What does xmlns:f="https://example.com/furniture" do?

Learn XML with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp