{"record":{"id":"39591de7ae235ced","repo":"karatelabs/karate","slug":"xmlpath-failed-for-path-path-message","errorCode":null,"errorMessage":"xmlPath failed for path: {path} - {message}","messagePattern":"xmlPath failed for path: (.+?) - (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":979,"sourceCode":"    static JavaInvokable xmlPath() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"xmlPath() needs two arguments: xml and path\");\n            }\n            Object xmlObj = args[0];\n            String path = args[1].toString();\n            Node doc;\n            if (xmlObj instanceof Node) {\n                doc = (Node) xmlObj;\n            } else if (xmlObj instanceof String) {\n                doc = Xml.toXmlDoc((String) xmlObj);\n            } else {\n                throw new RuntimeException(\"xmlPath() first argument must be XML node or string, but was: \" + (xmlObj == null ? \"null\" : xmlObj.getClass()));\n            }\n            try {\n                return evalXmlPath(doc, path);\n            } catch (Exception e) {\n                throw new RuntimeException(\"xmlPath failed for path: \" + path + \" - \" + e.getMessage(), e);\n            }\n        };\n    }\n\n    // ========== Control Flow Utilities ==========\n\n    /**\n     * karate.fail(message) - Explicitly fail the scenario with a message.\n     */\n    static JavaInvokable fail() {\n        return args -> {\n            String message = args.length > 0 && args[0] != null ? args[0].toString() : \"karate.fail() called\";\n            throw new RuntimeException(message);\n        };\n    }\n\n    // ========== Type Conversion Utilities (Invokable) ==========\n","sourceCodeStart":961,"sourceCodeEnd":997,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L961-L997","documentation":"karate.xmlPath() wraps any failure from the internal XPath evaluator (evalXmlPath) in a RuntimeException that includes the offending path and the underlying message. It is thrown whenever the XPath expression cannot be evaluated against the given XML document — bad syntax, no matching function, or a type-incompatible result.","triggerScenarios":"Calling karate.xmlPath(nodeOrString, path) where path is a malformed XPath expression, uses an unsupported function, or the first argument is valid XML but the expression cannot be evaluated (the preceding guard already rejects non-XML first args with a different message).","commonSituations":"Typos in XPath like '/foo/bar[@id]' missing a value; using Karate's JsonPath-style '$.' prefixes on XML; relying on XPath 2.0+ functions not supported by the underlying engine.","solutions":["Validate the XPath expression syntax; test it in an XPath playground against the actual XML","Remove JsonPath ($, $.) prefixes — xmlPath expects plain XPath, e.g. '/root/child'","Simplify the expression: use basic axis/node tests and supported functions only","Check the nested 'caused by' in the error for the precise XPath failure"],"exampleFix":"// before\nString v = karate.xmlPath(xml, '$.root.child');\n// after\nString v = karate.xmlPath(xml, '/root/child');","handlingStrategy":"try-catch","validationCode":"// JS: sanity-check the XPath before calling\nif (typeof path !== 'string' || !path.startsWith('/')) throw new Error('XPath must start with /: ' + path);","typeGuard":"// Java\nstatic boolean isXPath(String p) { return p != null && p.trim().startsWith(\"/\") && !p.startsWith(\"$.\"); }","tryCatchPattern":"try {\n    String v = karate.xmlPath(xml, '/root/child');\n} catch (Exception e) {\n    karate.logger.warn('xmlPath failed: {}', e.getMessage());\n    v = null;\n}","preventionTips":["Use plain XPath syntax (leading '/'), never JsonPath '$.' prefixes","Test XPath expressions in a playground against real payloads","Keep expressions to widely supported XPath 1.0 functions"],"tags":["xml","xpath","runtime-exception"],"backgroundTag":"invalid-argument-value","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"}