Quick revision
The short answer
A NamespaceContext tells the XPath engine what each prefix in an expression means. We implement getNamespaceURI(prefix) to return the SOAP 1.2 envelope URI for soap, urn:orders for o, XMLConstants.XML_NS_URI for xml and XMLConstants.NULL_NS_URI for anything unknown, and implement getPrefix and getPrefixes consistently for reverse lookup. We set it with xpath.setNamespaceContext(ctx) before compiling /soap:Envelope/soap:Body/o:Order/o:Id, on a document parsed with a namespace-aware factory.
What to remember
In this answer, we implemented NamespaceContext for SOAP 1.2 queries. getNamespaceURI maps soap to http://www.w3.org/2003/05/soap-envelope, o to urn:orders, xml and xmlns to their fixed URIs and anything else to XMLConstants.NULL_NS_URI, while getPrefix and getPrefixes provide the matching reverse lookups. We set the context on the XPath before compiling /soap:Envelope/soap:Body/o:Order/o:Id, saw that the document's own prefixes do not matter because only URIs are compared, and that parsing must be namespace-aware or nothing matches.
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.
A NamespaceContext tells the XPath engine what each prefix in an expression means. We implement getNamespaceURI(prefix) to return the SOAP 1.2 envelope URI for soap, urn:orders for o, XMLConstants.XML_NS_URI for xml and XMLConstants.NULL_NS_URI for anything unknown, and implement getPrefix and getPrefixes consistently for reverse lookup. We set it with xpath.setNamespaceContext(ctx) before compiling /soap:Envelope/soap:Body/o:Order/o:Id, on a document parsed with a namespace-aware factory.
Background to this question
Almost every Java service that reads SOAP messages or namespaced partner documents with XPath needs a NamespaceContext, and the JDK, surprisingly, does not ship a ready-made implementation. Many developers copy one from somewhere without knowing what each method is for, and then wonder why queries return nothing. In this answer, we will recap why XPath needs the mapping, write a complete implementation with the meaning of each method, use it on a real SOAP 1.2 message, and look at the details that decide whether anything matches: the URIs, the prefixes and namespace-aware parsing.
1Why XPath Needs a NamespaceContext
In a SOAP 1.2 message, the Envelope and Body elements belong to the namespace http://www.w3.org/2003/05/soap-envelope, and the payload to the service's own namespace, here urn:orders. An element's real name is the pair of namespace URI and local name. XPath 1.0, which the JDK implements, can only refer to a namespace through a prefix in the expression, and an unprefixed name in an expression always means no namespace.
So when we write soap:Envelope in an expression, the XPath engine must find out which URI soap stands for. It does not look at the document's own declarations for that; it asks the NamespaceContext we provide. Without one, compiling an expression with a prefix fails or matches nothing, depending on the implementation.
2Implementing the Interface
NamespaceContext, in javax.xml.namespace, has three methods. getNamespaceURI(prefix) is the forward lookup the XPath engine calls for every prefix in our expression. getPrefix(uri) and getPrefixes(uri) are reverse lookups from URI to prefix; XPath evaluation mainly uses the forward lookup, but other APIs and libraries may call the reverse ones, so we implement all three consistently.
The details follow the interface's contract. A null argument is an IllegalArgumentException. An unknown prefix returns XMLConstants.NULL_NS_URI, which is the empty string, meaning no namespace, rather than null. The xml and xmlns prefixes are fixed by the namespaces specification, so we return their standard URIs. Since Java 9, getPrefixes returns Iterator<String> rather than a raw Iterator. An immutable Map makes the class thread-safe, so one instance can be shared, unlike the XPath objects that use it.
3Using It on a SOAP 1.2 Message
Here is a SOAP 1.2 response and the code that extracts the order id from it.
9 messages in order. Play them, or step through them one at a time.
The document's own prefixes, env and ns2, play no part in resolving the expression.
Notice that the message uses the prefixes env and ns2, while our expression uses soap and o. It still matches, because the engine compares namespace URIs, not prefixes: env and soap both stand for the SOAP 1.2 URI, ns2 and o both stand for urn:orders. This is exactly what we want, because the partner's framework may generate different prefixes tomorrow.
4The Details That Decide Whether Anything Matches
Three mistakes cause most empty results. The first is parsing without namespace awareness. DocumentBuilderFactory is not namespace-aware by default, and then DOM nodes carry no namespace URIs, so a prefixed expression cannot match them. The second is the wrong URI. SOAP 1.1 uses http://schemas.xmlsoap.org/soap/envelope/ while SOAP 1.2 uses http://www.w3.org/2003/05/soap-envelope; a context built for one version silently fails on the other. URIs are compared exactly, so a trailing slash or different capitalisation also breaks matching.
The third is setting the context too late. setNamespaceContext() must be called before compile() or evaluate(), because prefixes are resolved when the expression is compiled. If a service must accept both SOAP versions, we can map two prefixes, say s11 and s12, and use a union such as /s11:Envelope/s11:Body/o:Order/o:Id | /s12:Envelope/s12:Body/o:Order/o:Id, or check the root's namespace first and pick the expression.
5Follow-up questions
Can I build the context from the document's own declarations?
We can walk the root element's attributes and collect its xmlns declarations, and some libraries do this. But it makes our expressions depend on the partner's prefixes, which defeats the point, and declarations can appear on inner elements too. A fixed context with our own prefixes is more robust.
Is there a ready-made implementation I can use?
The JDK has no public general-purpose implementation. Spring has SimpleNamespaceContext in its util.xml package, which lets us bind prefixes with bindNamespaceUri(). Writing our own small class is also perfectly fine and keeps the mapping explicit.
Why return NULL_NS_URI instead of null for unknown prefixes?
The interface contract says an unbound prefix returns XMLConstants.NULL_NS_URI. Returning null can cause a NullPointerException inside the XPath engine or be treated inconsistently between implementations, so following the contract keeps behaviour predictable.
6Conclusion
In this answer, we implemented NamespaceContext for SOAP 1.2 queries. getNamespaceURI maps soap to http://www.w3.org/2003/05/soap-envelope, o to urn:orders, xml and xmlns to their fixed URIs and anything else to XMLConstants.NULL_NS_URI, while getPrefix and getPrefixes provide the matching reverse lookups. We set the context on the XPath before compiling /soap:Envelope/soap:Body/o:Order/o:Id, saw that the document's own prefixes do not matter because only URIs are compared, and that parsing must be namespace-aware or nothing matches.
Open with
NamespaceContext is how I tell XPath what the prefixes in my expression mean, because XPath 1.0 can only refer to a namespace through a prefix and it does not use the document's own declarations for that.
Then, if they want more
The interface has three methods. getNamespaceURI takes a prefix and returns the URI: for soap I return the SOAP 1.2 envelope namespace, http www.w3.org 2003 05 soap-envelope, for o I return urn:orders, for xml the standard XML namespace from XMLConstants, and for anything unknown XMLConstants.NULL_NS_URI, the empty string, as the contract says; a null prefix is an IllegalArgumentException. getPrefix and getPrefixes are the reverse lookups from URI to prefix.
XPath evaluation mostly calls getNamespaceURI, but I implement the other two consistently from the same map. I back it with an immutable map, so one instance is thread-safe. Then I call xpath.setNamespaceContext before compiling /soap:Envelope/soap:Body/o:Order/o:Id.
The prefixes in my expression do not have to match the document's prefixes; if the message uses env and ns2, it still matches, because only the URIs are compared.
The two things I always check are that the DocumentBuilderFactory is namespace-aware, since it is off by default and then nothing matches, and that I use the right SOAP version's URI, because 1.1 and 1.2 differ.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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 "Write a NamespaceContext implementation for XPath queries over SOAP 1.2 messages (prefix...": https://scrivio-interview-library.netlify.app/java/xml-processing/9a0bc97655/