I have this failed test:
@Test
@SneakyThrows
public void testSimpleNsXpath() {
String str = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body xmlns=\"\"><h1>aaa</h1></body></html>";
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(str));
Document doc = db.parse(is);
XPath xpath = XPathFactory.newInstance().newXPath();
Node node = (Node) xpath.compile("/html[1]/body[1]/h1[1]").evaluate(doc, XPathConstants.NODE);
assertEquals("aaa", node.getTextContent());
}
I get null in node, if I remove xmlns test works.
How to fix my test? I wand to find h1 node.