1

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.

mystdeim
  • 4,802
  • 11
  • 49
  • 77
  • Removing the attribute `xmlns` programmatically with Jsoup also works: `Document doc = Jsoup.parse(html); Element htmlElement = doc.getElementsByTag("html").first(); if (htmlElement.hasAttr("xmlns")) {htmlElement.removeAttr("xmlns");}` –  Jul 16 '21 at 07:50

0 Answers0