XML interview question · Most asked
Your XPath query returns nothing, but the elements are clearly there. What's the likely cause?
Summary
The document has a default namespace, so its elements aren't in 'no namespace'. XPath 1.0 name tests without a prefix only match no-namespace elements. Bind a prefix to the namespace URI and use it in every step (//s:product/s:price). In Java also enable setNamespaceAware(true).
Longer Answer
This is the most common XML bug in integrations: a partner adds a namespace, queries silently return empty results, and an import "succeeds" with zero rows. Alert on unexpected zero counts.
Key Points
- Default namespace on the document.
- Prefix + namespace map in the query.
- Java: namespace-aware factory.
- Alert on empty results.
Deep dive: XML in Production: The Import That Found Zero Orders
Explanation
What interviewers look for
Namespace diagnosis and a production safeguard.
More most-asked XML questions
- What's the difference between well-formed and valid XML?
- Compare DOM, SAX and StAX (or pull parsing).
- Explain XML namespaces and the default namespace.
- What is an XXE attack and how do you prevent it?
- How does XSLT work? Explain templates and apply-templates.
- How would you process a 10 GB XML file?
- What is XML and where is it still used today?
- Which characters must be escaped in XML, and what is CDATA?