karatelabs/karate · error · RuntimeException

no results for xpath

Error message

no results for xpath: {path}

What it means

Xml.setByPath resolves the xpath to a target node and inserts/updates the given node there; if the xpath matches no node (getNodeByPath with create=true still returns null), it throws 'no results for xpath: {path}'.

Solutions

  1. Run the same xpath read-only first (Xml.getNodeByPath / evaluate) to confirm it matches before setting
  2. Print the actual document (doc to string) and verify element names, order, and indices against the path
  3. Remove or declare XML namespace prefixes correctly in the path
  4. Use an index within the number of existing nodes (e.g. /root/item[1] not [5] when only 2 exist)

Example fix

// before
Xml.setByPath(doc, "/Envelope/Body/Response/Discount", newNode); // Discount absent
// after
if (Xml.getNodeByPath(doc, "/Envelope/Body/Response", false) != null) {
    Xml.setByPath(doc, "/Envelope/Body/Response/Discount", newNode);
} else {
    throw new IllegalStateException("Response element missing from document");
}
Defensive patterns

Strategy: validation

Validate before calling

// pre-check that the xpath resolves before setByPath
Node probe = Xml.getNodeByPath(doc, path, false);
if (probe == null) {
    throw new IllegalStateException("xpath matches nothing, refusing set: " + path);
}

Try / catch

try {
    Xml.setByPath(doc, path, newNode);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("no results for xpath")) {
        throw new XmlPathException("document shape mismatch for " + path, e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling Xml.setByPath(doc, path, node) when the xpath does not resolve to any node in the document — misspelled element names, wrong namespaces, indices beyond existing siblings, or a path that create-on-demand could not materialize.

Common situations: Setting values in SOAP/XML responses using paths copied from a different document shape; namespace-prefixed xpaths where the prefix is not declared; 0-based vs 1-based index confusion on repeated elements.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/76bdc4ce269ca70c. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/common/Xml.java:433

    public static void setByPath(Node doc, String path, String value) {
        Node node = getNodeByPath(doc, path, true);
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            node.setNodeValue(value);
        } else if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
            node.getFirstChild().setTextContent(value);
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            node.setTextContent(value);
        }
    }

    public static void setByPath(Document doc, String path, Node in) {
        if (in.getNodeType() == Node.DOCUMENT_NODE) {
            in = in.getFirstChild();
        }
        Node node = getNodeByPath(doc, path, true);
        if (node == null) {
            throw new RuntimeException("no results for xpath: " + path);
        }
        Node newNode = doc.importNode(in, true);
        if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
            node.replaceChild(newNode, node.getFirstChild());
        } else {
            node.appendChild(newNode);
        }
    }

    public static void removeByPath(Document doc, String path) {
        Node node = getNodeByPath(doc, path, false);
        if (node == null) {
            return;
        }
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            Element parent = ((Attr) node).getOwnerElement();
            parent.removeAttribute(node.getNodeName());
        } else {

View on GitHub (pinned to a22eb90246)