{"record":{"id":"15e82eeacc5369e8","repo":"karatelabs/karate","slug":"xmlpath-needs-two-arguments-xml-and-path","errorCode":null,"errorMessage":"xmlPath() needs two arguments: xml and path","messagePattern":"xmlPath\\(\\) needs two arguments: xml and path","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":964,"sourceCode":"        if (value == null) {\n            return \"\";\n        }\n        if (value instanceof Node) {\n            return Xml.toString((Node) value, false);\n        }\n        return value.toString();\n    }\n\n    // ========== XML Utilities (Invokable) ==========\n\n    /**\n     * karate.xmlPath(xml, path) - Evaluate XPath on XML.\n     * First argument can be XML Node or String.\n     */\n    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    }","sourceCodeStart":946,"sourceCodeEnd":982,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L946-L982","documentation":"karate.xmlPath() evaluates an XPath expression against an XML document (a DOM Node or an XML string). Both the XML and the path are required, so the library throws when fewer than two arguments are supplied — without a path there is nothing to evaluate, and without XML there is nothing to evaluate it against.","triggerScenarios":"karate.xmlPath(xml) omitting the path; karate.xmlPath() with no arguments; passing the arguments as a single array/concatenated value so only one argument reaches the lambda.","commonSituations":"Asserting on SOAP/XML responses in tests; extracting values from XML config; migrating from karate's native xml path syntax to the explicit helper and dropping an argument.","solutions":["Supply both arguments: karate.xmlPath(xmlString, '/root/child').","Pass a DOM Node (e.g. from a variable already converted to XML) as the first argument if you have one.","Check the path expression is a separate string argument, not embedded in the XML."],"exampleFix":"// before\nvar val = karate.xmlPath('/foo/bar')\n// after\nvar val = karate.xmlPath(responseXml, '/foo/bar')","handlingStrategy":"validation","validationCode":"if (xml == null || path == null || typeof path !== 'string') { throw new Error('xmlPath(xml, path) requires an XML value and a string path') }","typeGuard":"function canXmlPath(xml, path) { return xml != null && typeof path === 'string' && path.length > 0 }","tryCatchPattern":"try { var val = karate.xmlPath(xml, path) } catch (e) { if (('' + e).indexOf('needs two arguments') >= 0) { throw new Error('xmlPath(): supply xml and xpath as two separate arguments') } throw e }","preventionTips":["Pass xml and path as two separate arguments","Confirm the XML variable is populated (not null/undefined) before the call","Keep xpath strings in named variables to avoid argument merging mistakes"],"tags":["javascript","argument-validation","xml","xpath"],"backgroundTag":"missing-required-argument","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"}