{"record":{"id":"76bdc4ce269ca70c","repo":"karatelabs/karate","slug":"no-results-for-xpath-path","errorCode":null,"errorMessage":"no results for xpath: {path}","messagePattern":"no results for xpath: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/common/Xml.java","lineNumber":433,"sourceCode":"\n    public static void setByPath(Node doc, String path, String value) {\n        Node node = getNodeByPath(doc, path, true);\n        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {\n            node.setNodeValue(value);\n        } else if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {\n            node.getFirstChild().setTextContent(value);\n        } else if (node.getNodeType() == Node.ELEMENT_NODE) {\n            node.setTextContent(value);\n        }\n    }\n\n    public static void setByPath(Document doc, String path, Node in) {\n        if (in.getNodeType() == Node.DOCUMENT_NODE) {\n            in = in.getFirstChild();\n        }\n        Node node = getNodeByPath(doc, path, true);\n        if (node == null) {\n            throw new RuntimeException(\"no results for xpath: \" + path);\n        }\n        Node newNode = doc.importNode(in, true);\n        if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) {\n            node.replaceChild(newNode, node.getFirstChild());\n        } else {\n            node.appendChild(newNode);\n        }\n    }\n\n    public static void removeByPath(Document doc, String path) {\n        Node node = getNodeByPath(doc, path, false);\n        if (node == null) {\n            return;\n        }\n        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {\n            Element parent = ((Attr) node).getOwnerElement();\n            parent.removeAttribute(node.getNodeName());\n        } else {","sourceCodeStart":415,"sourceCodeEnd":451,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/common/Xml.java#L415-L451","documentation":"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}'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the same xpath read-only first (Xml.getNodeByPath / evaluate) to confirm it matches before setting","Print the actual document (doc to string) and verify element names, order, and indices against the path","Remove or declare XML namespace prefixes correctly in the path","Use an index within the number of existing nodes (e.g. /root/item[1] not [5] when only 2 exist)"],"exampleFix":"// before\nXml.setByPath(doc, \"/Envelope/Body/Response/Discount\", newNode); // Discount absent\n// after\nif (Xml.getNodeByPath(doc, \"/Envelope/Body/Response\", false) != null) {\n    Xml.setByPath(doc, \"/Envelope/Body/Response/Discount\", newNode);\n} else {\n    throw new IllegalStateException(\"Response element missing from document\");\n}","handlingStrategy":"validation","validationCode":"// pre-check that the xpath resolves before setByPath\nNode probe = Xml.getNodeByPath(doc, path, false);\nif (probe == null) {\n    throw new IllegalStateException(\"xpath matches nothing, refusing set: \" + path);\n}","typeGuard":null,"tryCatchPattern":"try {\n    Xml.setByPath(doc, path, newNode);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"no results for xpath\")) {\n        throw new XmlPathException(\"document shape mismatch for \" + path, e);\n    }\n    throw e;\n}","preventionTips":["Dump and inspect the actual XML before scripting set paths","Keep xpath indices within the count of existing sibling nodes","Avoid namespace prefixes unless declared; use local-name() if needed","Derive paths from the live response instead of copying from samples"],"tags":["xml","xpath"],"backgroundTag":"empty-result-set","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}