1

I need to pass an argument into an xdmp:eval with xquery:

let $uri :="/tmp/myDoc.csv"

let $doc:= xdmp:eval('fn:doc($uri)' , (),  <options xmlns="xdmp:eval"><database>{xdmp:database("My-Database")}</database></options>) 

return $doc

But i'm getting Undefined variable $uri

I need to do this with an xdmp:eval for many reasons, have any one any idea how to do this in xquery ?

Yacino
  • 645
  • 2
  • 10
  • 32
  • 2
    See the answers in https://stackoverflow.com/questions/63147584/how-to-pass-the-map-variable-to-the-external-function-like-eval-and-invoke-in-ma, the query string needs the declare the variable. – Martin Honnen Aug 06 '20 at 12:11
  • 1
    Also, `xdmp:eval` is generally not your best option. Look to use `xdmp:invoke-function()` instead. It is safer, and easier. https://www.marklogic.com/blog/first-class-functions/ – Mads Hansen Aug 06 '20 at 15:02
  • @MadsHansen I tried the xdmp:invoke-function() , that resolve my issue , thanks – Yacino Aug 07 '20 at 07:19

1 Answers1

2

When you eval that string, it has no context to know that the $uri value should be. You can pass those context values in the second parameter when you invoke:

let $uri :="/tmp/myDoc.csv"
let $doc:= xdmp:eval('fn:doc($uri)', 
                     (xs:QName("uri"), $uri),  
                     <options xmlns="xdmp:eval">
                       <database>{xdmp:database("My-Database")}</database>
                     </options>) 
return $doc

But you should consider using xdmp:invoke-function() instead, with the anonymous function:

let $uri := "/tmp/myDoc.csv"
xdmp:invoke-function(function(){ fn:doc($uri) }, 
  <options xmlns="xdmp:eval">
    <database>{xdmp:database("My-Database")}</database>
  </options>
)

It is generally easier and safer to use xdmp:invoke-function.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147