karatelabs/karate · error · RuntimeException

xmlPath() needs two arguments: xml and path

Error message

xmlPath() needs two arguments: xml and path

What it means

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.

Solutions

  1. Supply both arguments: karate.xmlPath(xmlString, '/root/child').
  2. Pass a DOM Node (e.g. from a variable already converted to XML) as the first argument if you have one.
  3. Check the path expression is a separate string argument, not embedded in the XML.

Example fix

// before
var val = karate.xmlPath('/foo/bar')
// after
var val = karate.xmlPath(responseXml, '/foo/bar')
Defensive patterns

Strategy: validation

Validate before calling

if (xml == null || path == null || typeof path !== 'string') { throw new Error('xmlPath(xml, path) requires an XML value and a string path') }

Type guard

function canXmlPath(xml, path) { return xml != null && typeof path === 'string' && path.length > 0 }

Try / catch

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 }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:964

        if (value == null) {
            return "";
        }
        if (value instanceof Node) {
            return Xml.toString((Node) value, false);
        }
        return value.toString();
    }

    // ========== XML Utilities (Invokable) ==========

    /**
     * karate.xmlPath(xml, path) - Evaluate XPath on XML.
     * First argument can be XML Node or String.
     */
    static JavaInvokable xmlPath() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("xmlPath() needs two arguments: xml and path");
            }
            Object xmlObj = args[0];
            String path = args[1].toString();
            Node doc;
            if (xmlObj instanceof Node) {
                doc = (Node) xmlObj;
            } else if (xmlObj instanceof String) {
                doc = Xml.toXmlDoc((String) xmlObj);
            } else {
                throw new RuntimeException("xmlPath() first argument must be XML node or string, but was: " + (xmlObj == null ? "null" : xmlObj.getClass()));
            }
            try {
                return evalXmlPath(doc, path);
            } catch (Exception e) {
                throw new RuntimeException("xmlPath failed for path: " + path + " - " + e.getMessage(), e);
            }
        };
    }

View on GitHub (pinned to a22eb90246)