As noted in the comments, you have two options:
- Use
local-name() to refer to target nodes ignoring namespaces - Register all namespaces correctly with the XPath engine
Here's how to do it in Java:
XPath xpath = XPathFactory.newInstance().newXPath(); NamespaceContext ctx = new NamespaceContext() { public String getNamespaceURI(String prefix) { if ("v20".equals(prefix)) { return "testNS1"; } else if ("xsi".equals(prefix)) { return "http://www.w3.org/2001/XMLSchema-instance"; } return null; } public String getPrefix(String uri) { throw new UnsupportedOperationException(); } public Iterator getPrefixes(String uri) { throw new UnsupportedOperationException(); } }; xpath.setNamespaceContext(ctx); XPathExpression expr = xpath.compile("//v20:Body/@xsi:type"); System.out.println(expr.evaluate(doc, XPathConstants.STRING));
Please note that I accept the following namespace declarations:
<v20:MessageV1Request xmlns:v20="testNS1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
You will need to update getNamespaceURI to use the actual values.
source share