{"record":{"id":"20e5f49244872dec","repo":"karatelabs/karate","slug":"setxml-needs-at-least-two-arguments-name-and-xml","errorCode":null,"errorMessage":"setXml() needs at least two arguments: name and xml","messagePattern":"setXml\\(\\) needs at least two arguments: name and xml","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":781,"sourceCode":"            Object var = engine.get(varName);\n            if (var instanceof Node && path != null && path.startsWith(\"/\")) {\n                // XPath remove on XML\n                Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();\n                Xml.removeByPath(doc, path);\n            } else if ((var instanceof Map || var instanceof List) && path != null) {\n                // Route through Json so nested paths (props.field3), JsonPath ($.props.field3)\n                // and bracketed keys all resolve - mirroring the `remove` keyword. A bare\n                // top-level key is normalized to a JsonPath by Json.prefix().\n                Json.of(var).remove(path);\n            }\n            return null;\n        };\n    }\n\n    private JavaInvokable setXml() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"setXml() needs at least two arguments: name and xml\");\n            }\n            String name = args[0].toString();\n            if (args.length == 2) {\n                // Simple form: setXml('name', '<xml/>')\n                String xml = args[1].toString();\n                engine.put(name, Xml.toXmlDoc(xml));\n            } else {\n                // Path form: setXml('name', '/path', '<xml/>')\n                String path = args[1].toString();\n                String xml = args[2].toString();\n                Object target = engine.get(name);\n                if (target instanceof Node) {\n                    Node doc = (Node) target;\n                    if (doc.getNodeType() != Node.DOCUMENT_NODE) {\n                        doc = doc.getOwnerDocument();\n                    }\n                    Xml.setByPath((org.w3c.dom.Document) doc, path, Xml.toXmlDoc(xml));\n                }","sourceCodeStart":763,"sourceCodeEnd":799,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L763-L799","documentation":"karate.setXml(name, xml) parses an XML string into a document node and assigns it to the named variable; an optional third argument adds a path/value operation. The library throws this error when fewer than two arguments are given, since both the variable name and the XML content are required.","triggerScenarios":"Calling karate.setXml(name) with no XML argument, or with none at all — commonly when the XML string comes from an undefined variable, or when confusing setXml with set and omitting the value.","commonSituations":"Building XML dynamically from a response where the extracted value is undefined; migrating karate.set calls to setXml without updating arity; templated XML that interpolated to empty/undefined.","solutions":["Pass both arguments: karate.setXml('myXml', '<root><a>1</a></root>').","If the XML is derived from another variable, coerce and verify it first: karate.setXml('myXml', String(xmlString)).","If assigning a non-XML value, use karate.set(name, value) instead.","Optionally pass a third argument for path-based updates: karate.setXml('myXml', '<x/>', '/x/a', '1')."],"exampleFix":"// before\nkarate.setXml('myXml')\n\n// after\nkarate.setXml('myXml', '<root><item>value</item></root>')","handlingStrategy":"validation","validationCode":"// JS, before calling\nif (typeof xmlString !== 'string' || xmlString.trim().length === 0) {\n  throw new Error('setXml() requires a non-empty XML string');\n}\nkarate.setXml('myXml', xmlString);","typeGuard":"function isNonEmptyXml(s) { return typeof s === 'string' && s.trim().startsWith('<'); }","tryCatchPattern":"try {\n  karate.setXml('myXml', xmlString);\n} catch (e) {\n  if (String(e.message).indexOf('setXml() needs at least two arguments') !== -1) {\n    throw new Error('setXml() missing name or xml — xml resolved to: ' + xmlString);\n  }\n  throw e;\n}","preventionTips":["Never omit the xml argument even when the value is dynamically generated.","Use karate.set for non-XML values to avoid arity/format confusion.","When XML comes from a response, extract and stringify it before assignment."],"tags":["xml","argument-count","karate"],"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"}