I can confirm that your test fails - and don't have a good explanation for it, sorry.
Note that the namespace context is the context for prefixes used inside your XPath, not for prefixes of the document. If you are free to chose your XPath you could as well use a non-default namespace, something like
as XPath expression (note the colon with no prefix) and at least with standard JAXP libs it seems to work as well.
Your "use any namespace prefix" advice is exactly what I did in the testcase in svn to show that it worked.
I cannot find any explicit documentation that says you must use namespace prefixes in the XPath expression if your document is using namespaces, but it seems to be true nonetheless.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I want to create a test which checks for atribute value. The XML uses a namespace. The test bellow fails. Why?
@Test
public void checkVersion() throws ParserConfigurationException, SAXException, IOException, XpathException {
String t = "<a xmlns=\"http://www.acme.com\"><b c='cv'>bv</b></a>";
Document d = XMLUnit.buildControlDocument(t);
HashMap m = new HashMap();
m.put("", "http://www.acme.com");
NamespaceContext ctx = new SimpleNamespaceContext(m);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
XMLAssert.assertXpathEvaluatesTo("bv", "//b", d);
}
Thanks for any input,
Borut
I can confirm that your test fails - and don't have a good explanation for it, sorry.
Note that the namespace context is the context for prefixes used inside your XPath, not for prefixes of the document. If you are free to chose your XPath you could as well use a non-default namespace, something like
http://xmlunit.svn.sourceforge.net/viewvc/xmlunit/branches/xmlunit-1.x/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java?r1=292&r2=345
Stefan
Well, if i am not mistake, you need to have something for your prefix. Empty string will not cut it. I would do this:
Then, you would need to change you xpath expression:
Or you can rewrite the above to look like this :
Note, all credit goes to http://blog.davber.com/2006/09/17/xpath-with-namespaces-in-java/
- Stefan Baramov
Thanks, Stefan. The davber post made me try
as XPath expression (note the colon with no prefix) and at least with standard JAXP libs it seems to work as well.
Your "use any namespace prefix" advice is exactly what I did in the testcase in svn to show that it worked.
I cannot find any explicit documentation that says you must use namespace prefixes in the XPath expression if your document is using namespaces, but it seems to be true nonetheless.