Quick revision
The short answer
In the v2 file the default namespace declaration puts every unprefixed element, including order and id, into the namespace urn:shop:v2. In XPath 1.0 an unprefixed name always means an element in no namespace, so //order no longer matches anything. We fix it by parsing with a namespace-aware DocumentBuilderFactory, registering a NamespaceContext that maps a prefix such as s to urn:shop:v2, and querying //s:order/s:id.
What to remember
In this answer, we saw that xmlns="urn:shop:v2" puts every unprefixed element of the v2 document into that namespace, while in XPath 1.0 an unprefixed name means no namespace, so //order/id silently matches nothing. The fix is to parse with setNamespaceAware(true), which is off by default, register a NamespaceContext mapping a prefix like s to urn:shop:v2, and query //s:order/s:id, prefixing every step. The local-name() trick works but ignores namespaces and is slower, and absolute paths beat // on large documents.
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.
In the v2 file the default namespace declaration puts every unprefixed element, including order and id, into the namespace urn:shop:v2. In XPath 1.0 an unprefixed name always means an element in no namespace, so //order no longer matches anything. We fix it by parsing with a namespace-aware DocumentBuilderFactory, registering a NamespaceContext that maps a prefix such as s to urn:shop:v2, and querying //s:order/s:id.
Why this question is asked
This bug appears the day a partner upgrades their format. The documents look the same to the eye, only one attribute on the root has changed, and suddenly every query returns nothing, without a single error. Interviewers like it because it tests whether we really understand namespaces in XPath, which many experienced developers find confusing. In this answer, we will recap what the default namespace does, reproduce the empty result, explain why XPath 1.0 behaves this way, apply the proper fix in Java step by step, look at the local-name() workaround and its costs, and mention the performance of //.
1A Quick Recap of the Default Namespace
A namespace gives element names a global identity: the pair of a namespace URI and a local name. xmlns="urn:shop:v2" on the root declares a default namespace, which means every unprefixed element inside it, the root itself, every order and every id, belongs to urn:shop:v2, even though the tags themselves look exactly like the v1 tags.
So for a namespace-aware parser, the v1 <order> is the name (no namespace, order), and the v2 <order> is the name (urn:shop:v2, order). To software that respects namespaces these are different names, just as two classes called Order in different Java packages are different classes.
Notice that nothing inside the document changed. Only the declaration on the root did, and it changes the identity of every element below it.
2Why //order Matches Nothing
In XPath 1.0, a name test without a prefix, such as order, matches elements whose name is order and whose namespace is none. There is no way to say 'the default namespace' in an XPath 1.0 expression; the default namespace declared in the document does not apply to the expression. So //order asks for (no namespace, order) and the document only has (urn:shop:v2, order). Result: empty node set, and no error, because asking for something that is not there is not an error.
XPath 2.0 and later, available in Java through Saxon, allow setting a default element namespace for expressions, but the JDK's javax.xml.xpath implements XPath 1.0, so we need prefixes.
| Query | Asks for | v1 file has | v2 file has | Match? |
|---|---|---|---|---|
| //order | (none, order) | (none, order) | (urn:shop:v2, order) | v1 only |
| //s:order with s = urn:shop:v2 | (urn:shop:v2, order) | (none, order) | (urn:shop:v2, order) | v2 only |
| //*[local-name()='order'] | any namespace, order | (none, order) | (urn:shop:v2, order) | both |
3First, Parse With Namespace Awareness
DocumentBuilderFactory is not namespace-aware by default; setNamespaceAware(false) is the out-of-the-box setting, a historical choice that surprises almost everybody. Without namespace awareness the DOM nodes do not carry namespace URIs at all, so no prefix mapping can ever match them, and how XPath treats such nodes varies between implementations. So the first step is always to turn it on.
4Then Map a Prefix With a NamespaceContext
XPath needs to know what a prefix in the expression means. We provide that through a NamespaceContext. The prefix we choose in the expression does not have to appear in the document; the document uses no prefix at all. Only the URI must match.
Notice that both steps of the path need the prefix. //s:order/id would fail again, because id is also in urn:shop:v2. If one service must read both v1 and v2 files, we can either detect the version from the root's namespace and choose the expression, or write a union such as //s:order/s:id | //order/id.
5The local-name() Workaround and Its Costs
A common shortcut is //*[local-name()='order']/*[local-name()='id']. It matches elements by local name only, in any namespace, so it works for both files without a NamespaceContext.
It has two costs. First, it throws away the meaning of namespaces: if a document ever mixes vocabularies, for example an order from another schema embedded in an extension, the query matches both. Second, it is slower, because the processor must test every element with a function call instead of using a direct name test, and with // that means every element in the document. It is acceptable for a quick investigation, but for production code the prefix mapping is the correct fix.
6A Word on // and Performance
Whichever form we use, // means descendant at any depth, so the processor walks the whole tree. On a 50 KB message that does not matter. On a 200 MB document evaluated many times it does. If we know the structure, an absolute path such as /s:orders/s:order/s:id visits only the nodes on that path and states our intent more precisely, which also protects us from accidentally matching an order element nested somewhere unexpected.
7Follow-up questions
Why did the v1 query work even though the parser might have been namespace-aware?
Because in the v1 file the elements really are in no namespace, so (no namespace, order) matches exactly. Namespace awareness only changes things when the document declares namespaces, which is why the problem appeared with v2.
Can I set a default namespace for XPath expressions in Java?
Not with the JDK's XPath 1.0 implementation. Saxon's XPath 2.0 and 3.x APIs allow a default element namespace for the expression, so unprefixed names in the query mean urn:shop:v2. With the JDK, we always use prefixes.
Do I need to implement getPrefix and getPrefixes?
XPath evaluation mostly calls getNamespaceURI, but implementing all three consistently is good practice, and some libraries call the reverse lookups. Returning null from getPrefix and an empty iterator from getPrefixes for unknown URIs is correct.
8Conclusion
In this answer, we saw that xmlns="urn:shop:v2" puts every unprefixed element of the v2 document into that namespace, while in XPath 1.0 an unprefixed name means no namespace, so //order/id silently matches nothing. The fix is to parse with setNamespaceAware(true), which is off by default, register a NamespaceContext mapping a prefix like s to urn:shop:v2, and query //s:order/s:id, prefixing every step. The local-name() trick works but ignores namespaces and is slower, and absolute paths beat // on large documents.
Open with
The v2 root declares a default namespace, xmlns equals urn:shop:v2, so every unprefixed element in the document, orders, order and id, now belongs to that namespace even though the tags look identical.
Then, if they want more
In XPath 1.0, which is what the JDK implements, an unprefixed name in an expression always means an element in no namespace; the document's default namespace does not apply to the query. So //order asks for something that is not there and returns an empty result without any error. In v1 there was no namespace, so it matched.
To fix it in Java, first I make sure the DocumentBuilderFactory is namespace-aware, because setNamespaceAware is false by default and without it nodes do not even carry namespace URIs.
Then I set a NamespaceContext on the XPath object that maps a prefix of my choice, say s, to urn:shop:v2, and I write the query as //s:order/s:id, with the prefix on every step. The prefix does not need to exist in the document, only the URI must match.
The quick workaround is local-name equals order, which matches any namespace, but it loses the safety of namespaces and is slower because it tests every element. I would also replace // with an absolute path like /s:orders/s:order/s:id on large documents, because // scans the whole tree.
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 "An XPath expression //order/id returns nothing for a document whose root is <orders...": https://scrivio-interview-library.netlify.app/java/xml-processing/a26b05b6bf/