Quick revision
The short answer
Namespaces let one document combine vocabularies without name collisions, by giving every element name a global identity: a namespace URI plus a local name. The URI is the identity, a prefix is only a local alias for it, and a default namespace (xmlns="...") applies to unprefixed elements within its scope. Unprefixed attributes are not in the default namespace; they are in no namespace at all. The URI does not have to point to anything; it is simply a unique string.
What to remember
In this answer, we saw that namespaces prevent name collisions when vocabularies are combined, by making an element's name the pair of namespace URI and local name. The URI is the identity and the prefix is only a local alias, so a:item and b:item are the same when both prefixes map to the same URI. A default namespace applies to unprefixed elements within its scope, unprefixed attributes are in no namespace, and the URI is just a unique string compared character by character that never needs to resolve.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Namespaces let one document combine vocabularies without name collisions, by giving every element name a global identity: a namespace URI plus a local name. The URI is the identity, a prefix is only a local alias for it, and a default namespace (xmlns="...") applies to unprefixed elements within its scope. Unprefixed attributes are not in the default namespace; they are in no namespace at all. The URI does not have to point to anything; it is simply a unique string.
Why this question is asked
Namespaces are behind a surprising number of XML bugs: XPath queries that return nothing, JAXB classes that refuse to unmarshal and SAX handlers that work for one partner but not another. Most of these come from treating the prefix as the name. Interviewers ask this concept question to see whether we know what really identifies an element. In this answer, we will see the collision problem namespaces solve, separate the URI from the prefix with an example, look at how the default namespace scopes, settle the attribute rule that trips up many people, and see how Java exposes all of this.
1The Problem: Two Vocabularies, One Name
Imagine an order document that includes a book from a publisher's catalog. The order format has a <title> for the customer's title, such as Dr, and the catalog format has a <title> for the book's name. If we put both into one document, a program that sees <title> cannot know which one it is.
Namespaces solve this by qualifying names. Each vocabulary picks a namespace URI that it owns, for example urn:shop:orders and http://example.com/catalog. An element's real name becomes the pair (namespace URI, local name), so (urn:shop:orders, title) and (http://example.com/catalog, title) are different names, exactly as two classes named Title in different Java packages are different classes.
2URI, Prefix and Default Namespace
We declare namespaces with xmlns attributes. xmlns:o="urn:shop:orders" binds the prefix o to that URI, so o:title means (urn:shop:orders, title). xmlns="http://example.com/catalog", with no prefix, declares a default namespace: from that element down, every unprefixed element is in that namespace.
| Written as | Namespace URI | Local name |
|---|---|---|
| o:title | urn:shop:orders | title |
| book | http://example.com/catalog (default) | book |
| title inside book | http://example.com/catalog (default) | title |
| isbn attribute on book | none | isbn |
| a:item or b:item | urn:shop:orders | item |
The last two lines are the key point. The prefixes a and b differ, but both map to the same URI, so the elements have the same name. The prefix is only a local abbreviation chosen by the writer, valid within the element where it is declared and its descendants. A partner can switch from o: to ord: tomorrow without changing the meaning of their documents, and a correct program will not notice.
3How the Default Namespace Scopes
A namespace declaration applies to the element that carries it and everything inside it, until another declaration overrides it. An inner element can declare xmlns="urn:other" and change the default for its own subtree, or undeclare it with xmlns="", which puts its unprefixed children back into no namespace.
This is why a document can look unchanged while every name in it has changed: adding xmlns="urn:shop:v2" to the root moves every unprefixed element into urn:shop:v2. Software that matches names without namespaces may still work by accident; software that respects namespaces, such as XPath with a namespace-aware DOM or Jakarta XML Binding, will see completely different names.
4Are Attributes in the Default Namespace?
No, and this is the rule people most often get wrong. The default namespace applies to unprefixed elements only. An unprefixed attribute, like isbn above, is in no namespace, even though its element is in the catalog namespace. It is understood as belonging to its element, and that is usually enough.
If an attribute must be in a namespace, it needs a prefix, for example xml:lang or xsi:type, or a vendor attribute such as o:priority="high" on a catalog element. In Java this affects how we read it: getAttributeNS(null, "isbn") or simply getAttribute("isbn") for the unprefixed one, and getAttributeNS("urn:shop:orders", "priority") for the prefixed one. The same rule applies in XPath, where @isbn matches the no-namespace attribute and @o:priority needs a prefix mapping.
5The URI Is Just a Unique String
A namespace URI often looks like a web address, http://www.w3.org/2003/05/soap-envelope for example, but the parser never fetches it. It is compared as a string, character by character, so http://example.com/catalog and http://example.com/catalog/ with a trailing slash are different namespaces. Organisations use URLs because they own their domain, which keeps the strings unique, and some publish documentation at that address. A URN such as urn:shop:orders works equally well.
In Java, a namespace-aware parser reports the URI and local name for every node: getNamespaceURI() and getLocalName() in DOM, the uri and localName parameters in SAX, getNamespaceURI() and getLocalName() in StAX, and the QName class combines both. Code that matches on these, rather than on prefixed names, is correct for any choice of prefixes.
6Follow-up questions
Why does xml:lang work without any declaration?
The prefix xml is predeclared by the XML Namespaces specification and is always bound to http://www.w3.org/XML/1998/namespace. It cannot be bound to anything else. Similarly, xmlns is reserved for declarations themselves.
Is xmlns itself an attribute in a namespace?
In the namespace-aware view, namespace declarations are treated specially; DOM exposes them as attributes in the namespace http://www.w3.org/2000/xmlns/, while SAX by default reports them through startPrefixMapping events rather than as attributes. Most application code never needs to read them directly.
Can two prefixes for the same URI appear in one document?
Yes, it is legal, and the elements are the same names. Tools that canonicalise or pretty-print XML may rewrite prefixes, which is harmless for correct consumers and one more reason never to depend on prefixes.
7Conclusion
In this answer, we saw that namespaces prevent name collisions when vocabularies are combined, by making an element's name the pair of namespace URI and local name. The URI is the identity and the prefix is only a local alias, so a:item and b:item are the same when both prefixes map to the same URI. A default namespace applies to unprefixed elements within its scope, unprefixed attributes are in no namespace, and the URI is just a unique string compared character by character that never needs to resolve.
Open with
Namespaces solve the collision problem when a document combines vocabularies. If an order format and a book catalog both have a title element, a program cannot tell them apart by name alone.
Then, if they want more
So each vocabulary has a namespace URI, and an element's real name is the pair of URI and local name. The prefix is just a local alias the writer chooses: if a and b both map to urn:shop:orders, then a:item and b:item are exactly the same element name, and the partner can rename their prefix any day without changing meaning.
A default namespace, declared with xmlns equals something and no prefix, applies to every unprefixed element in that element's subtree, until something inside overrides it. That is why adding a default namespace to a root changes the name of every element in the file even though the text looks the same.
The part people often get wrong is attributes: an unprefixed attribute is not in the default namespace, it is in no namespace. If an attribute needs a namespace, it needs a prefix. And the URI does not have to resolve; the parser never fetches it, it just compares strings, so even a trailing slash makes it a different namespace.
In Java, I parse with a namespace-aware factory and match on namespace URI and local name, never on the prefix.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What problem do XML namespaces solve, and what is the difference between a prefix, a...": https://scrivio-interview-library.netlify.app/java/xml-processing/05a845ac7c/