2

I had an issue with with writing XPath using XMLUnit 2.0.0 and a default namespace. Here is my sample xml:

<a xmlns="uri:foo">
    <b>value</b>
</a>

I couldn't access any elements with the following XMLUnit XPath code:

XPathEngine engine = new JAXPXPathEngine();
engine.setNamespaceContext(new HashMap<String, String>(1) {{
    put(DEFAULT_NS_PREFIX, "uri:foo");
}});
assertEquals("value", engine.evaluate("/a/b",
             Input.fromString("<a xmlns=\"uri:foo\"><b>value</b></a>").build()));

How do I access elements using XPathEngine/XPath using the default namespace?

1 Answers1

1

The answer I found was to prefix the elements in the XPath with a single ':'.

"/:a/:b"

That solved my problem. In summary, I did the following:

XPathEngine engine = new JAXPXPathEngine();
engine.setNamespaceContext(new HashMap<String, String>(1) {{
    put(DEFAULT_NS_PREFIX, "uri:foo");
}});
assertEquals("value", engine.evaluate("/:a/:b",
             Input.fromString("<a xmlns=\"uri:foo\"><b>value</b></a>").build()));