Passing Variable to Xdmp: Eval Marklogic Xquery

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 ?

3

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest