Search code examples
javaxpathnamespacesxmlunit

Namespace issue in xpath using XMLUnit 1.6


I'm using XMLUnit 1.6 for testing my xsl trasfromations.Mentioned below are two sample tests. In test1(), xml is NOT having namespace. test1 is getting passed which is expected. In test2(), xml is having namespace and test is getting failed.I'm not able to give the correct xpath that is expected in the second argument of assertXpathEvaluatesTo("expectedValue","xpath","ResultXML").

@Test
public void test1() throws Exception {

    String resultXML =  "<MyXML><Value>3</Value></MyXML>";
    XMLAssert.assertXpathEvaluatesTo("3","//Value",resultXML);
}

@Test
public void test2() throws Exception {

    String resultXML =  "<tns:MyXML xmlns:tns='testns'><tns:Value>3</tns:Value></tns:MyXML>";
    XMLAssert.assertXpathEvaluatesTo("3","//{testns}Value",resultXML);

}

Could anybody please help with the correct value for xpath . Thanks in advance.


Solution

  • You could use the xpath : //*[local-name()='Value']/text()