1

xdmp:eval($s, (), "world"))

Instead of default value as second parameter from the above code, i want to pass map as the parameter to the external function.

Ruby Kannan
  • 251
  • 1
  • 6
  • Your example code doesn't look correct. You are passing an empty sequence for the `$vars` and the string "world" as the `$options` and have an extra parenthesis. If modeling from the examples, likely meant to post `xdmp:eval($s, (xs:QName("my:x"), "world"))` and then it might be more clear what you were asking. – Mads Hansen Jul 29 '20 at 17:13

2 Answers2

1

If $s has e.g. declare variable $v1 external; in the prolog then I think you can use xdmp:eval($s, map:new(map:entry('{}v1', some-expression))), or, as pointed out in a comment, even xdmp:eval($s, map:new(map:entry('v1', some-expression))) to pass the result of evaluating some-expression as the value of the variable $v1 e.g. xdmp:eval($s, map:new(map:entry('v1', 'foo'))) passes in the string value foo.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

As indicated in the documentation for xdmp:eval() you can either pass a sequence of alternating QName and value, or you can pass a map.

External variable values to make available to the evaluated code, expressed as either a sequence of alternating QName-value pairs, or a map:map.

  • If you use a sequence, it must contain alternating variable QNames and values. e.g. (xs:QName("var1"), "val1", xs:Qname("var2"), "val2").
  • If you use a map, then each key is a string representing the Clark notation of the variable QName ("{namespaceURI}localname"), and its value is the corresponding variable value. You can use xdmp:key-from-QName to generate the Clark notation to use as a key.

The example from xdmp:eval() documentation shows how to invoke with the sequence:

xquery version "1.0-ml";
declare namespace my='http://mycompany.com/test';

let $s :=
      "xquery version '1.0-ml';
       declare namespace my='http://mycompany.com/test';
       declare variable $my:x as xs:string external;
       declare variable $my:y as xs:string external := 'goodbye';
       concat('hello ', $my:x, ' ', $my:y)"
return
    (: evaluate the query string $s using the variables
       supplied as the second parameter to xdmp:eval :)
    xdmp:eval($s, (xs:QName("my:x"), "world"))

So, instead of (xs:QName("my:x"), "world") you need to create a map:map and create an entry with the key of the QName as Clark notation: {http://mycompany.com/test}x and the value "world" for that entry.

The way to invoke with a map:map would be:

let $vars := map:new() => map:with(xdmp:key-from-QName(xs:QName("my:x")), "world")
return
  xdmp:eval($s, $vars)
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • General note: it saves some hassle if you don't prefix your variable names. An external variable named `$x` can be referenced with `map:entry('x', 'world')`. – grtjn Jul 29 '20 at 19:08