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 Answer 1

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()));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.