Loading TOC...

xdmp:javascript-eval

xdmp:javascript-eval(
   $javascript as xs:string,
   [$vars as item()*],
   [$options as node()?]
) as item()*

Summary

Returns the result of evaluating a string as a JavaScript module.

Parameters
javascript The JavaScript string to be evaluated. If the JavaScript string contains double quotes ("), surround the string with single quotes (').
vars The external variable values for this evaluation. This can either be a sequence of map:map objects, or a sequence of even length, alternating name and items.

Each key in the map(s) is a string representing the name of the parameter. Each entry in the map is the value of the corresponding external variable.

Alternatively, the alternating sequence should contain name string and item pairs that specify a variable name and value for an external variable.

options The options node. The default value is (). The node for the xdmp:eval options must be in the xdmp:eval namespace. The following is a sample options node:
    <options xmlns="xdmp:eval">
      <isolation>different-transaction</isolation>
      <prevent-deadlocks>true</prevent-deadlocks>
    </options> 

The xdmp:eval options include:

<database>

The database ID, from xdmp:database("db_name"), xdmp:security-database(), or xdmp:schema-database(). To specify a database other than the context database, the http://marklogic.com/xdmp/privileges/xdmp-eval-in privilege is required.

<modules>

The modules database ID for processing module imports. Specifying no <modules> element in the options node specifies the current modules database. Specifying 0 specifies using the file system to process module imports. You must have the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change (for xdmp:eval), http://marklogic.com/xdmp/privileges/xdmp-invoke-modules-change (for xdmp:invoke), or the http://marklogic.com/xdmp/privileges/xdmp-spawn-modules-change (for xdmp:spawn) privilege to change either the modules database to another database or to change the root to another path. You must have the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change-file (for xdmp:eval), http://marklogic.com/xdmp/privileges/xdmp-invoke-modules-change-file (for xdmp:invoke), or the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change-file (for xdmp:spawn) privilege to change the modules database to the file system or to change a root on the filesystem.

<root>

The root path for modules. Specifying no <root> element in the options node specifies the current root. You need the same privileges to modify the root as you need to change the module option.

<timestamp>

The system timestamp to use for this evaluation. Specifying no <timestamp> element in the options node specifies the most recent timestamp. You may only specify a timestamp for a query statement, not for an update statement. The timestamp is a number that is incremented by 1 each time any configuration or content change is made to the system. Specifying a timestamp of 0 uses the current system timestamp (the value returned by xdmp:request-timestamp()). Specifying a timestamp requires the xdmp:timestamp execute privilege.

<ignore-amps>

(Only valid with xdmp:eval, xdmp:invoke, xdmp:xslt-eval, xdmp:xslt-invoke; does not apply to xdmp:spawn, dbg:eval or dbg:invoke) Either true or false. When set to true, the statement is evaluated without using any Amps from the caller. The default value for the ignore-amps option is false.

<isolation>

(Only valid with xdmp:eval or xdmp:invoke; does not apply to xdmp:spawn.) Either same-statement or different-transaction. When set to same-statement, the statement is evaluated in the same transaction as the one from which it is called, and subsequent expressions in the calling statement will not see any updates performed in the eval/invoke/spawn. You can only use same-statement isolation with update statements; query statements with same-statement isolation will throw an exception. When set to different-transaction, the statement is evaluated in a separate transaction from the one in which it is called, making those updates available to subsequent expressions in the calling statement (assuming the calling statement is an update statement; if the calling statement is not an update, then subsequent expressions will see the version of the database at the system timestamp when the calling statement begins its evaluation). When using different-transaction in an update statement that calls another update statement, do not update the same document as the calling statement is updating; doing so can cause a deadlock. You cannot evaluate a statement in a different database with the isolation option set to same-statement. The default value for the isolation option is different-transaction. For more details, see the "Understanding Transactions in MarkLogic Server" chapter of the Developer's Guide.

<static-check>

Specify true to only perform static checks on the module, and not to actually execute it.

<prevent-deadlocks>

(Only valid with xdmp:eval or xdmp:invoke; does not apply to xdmp:spawn.) Specify true for the server to disallow update requests from an update transaction. Only has an effect when the isolation option is set to different-transaction as there is no possibility of a deadlock if the isolation option is set to same-statement. When set to true in an update request calling another update request, MarkLogic Server throws the XDMP-PREVENTDEADLOCKS exception. Setting this option to true prevents the possibility of deadlocks occurring when running eval/invoke of an update transaction from another update transaction. The default value for the prevent-deadlocks option is false.

<time-limit>

Override the default time limit with this time limit, in seconds, for this evaluation. You can set the value up to the maximum-time-limit value for the App Server in which the request is evaluated or to a lower value than the default time limit. This option only applies to xdmp:spawn, not to xdmp:invoke or xdmp:eval.

<user-id>

Specifies the user ID for the user to run the request being evaluated (the request specified in the $xquery parameter). If no user-id is specified, then the request is run as the current user. You need to have the xdmp:login (http://marklogic.com/xdmp/privileges/xdmp-login) privilege to use the user-id option. Be aware that this is a very privileged operation, as it allows a user with this privilege to evaluate requests as any other user. For an example, see the fourth example below.

<default-collation>

Specifies the collation to use for this context, unless a collation is explicitly specified in the XQuery prolog or in a function call that allows you to specify a collation.

<priority>

Specify the priority of the spawned task. Allowable values for this option are "normal" and "higher". This option only applies to xdmp:spawn, not to xdmp:invoke or xdmp:eval.

<transaction-mode>

Explicitly set the transaction mode for this context. Valid values are auto, query, update-auto-commit and update. An xdmp:transaction-mode prolog option in the evaluated query will override any transaction mode specified with this option.

Required Privileges

http://marklogic.com/xdmp/privileges/xdmp-eval

You must have the http://marklogic.com/xdmp/privileges/xdmp-eval-in privilege to specify the <database> option with a database other than the context database.

You must have the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change (for xdmp:eval), http://marklogic.com/xdmp/privileges/xdmp-invoke-modules-change (for xdmp:invoke), or the http://marklogic.com/xdmp/privileges/xdmp-spawn-modules-change (for xdmp:spawn) privilege to change either the modules database to another database or to change the root to another path. You must have the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change-file (for xdmp:eval), http://marklogic.com/xdmp/privileges/xdmp-invoke-modules-change-file (for xdmp:invoke), or the http://marklogic.com/xdmp/privileges/xdmp-eval-modules-change-file (for xdmp:spawn) privilege to change the modules database to the file system or to change a root on the filesystem.

You must have the http://marklogic.com/xdmp/privileges/xdmp-login privilege to use the user-login option.

Example

  xdmp:javascript-eval("1+1")
  => 2

Example

let $foo := 
  'var mycars = xdmp.arrayValues(["Saab","Volvo","BMW","BMW"]); 
   fn.distinctValues(mycars);'
return
xdmp:javascript-eval($foo)
  => Saab,Volvo,BMW

Example

  xdmp:javascript-eval("fn.doc('/docs/mydoc.xml')",  (),
          <options xmlns="xdmp:eval">
            <database>{xdmp:database("otherdb")}</database>
          </options>)
  => The '/docs/mydoc.xml' document from the
     otherdb database.

Example

xdmp:javascript-eval('xdmp.getCurrentUser()',
 <options xmlns="xdmp:eval">
  <user-id>{xdmp:user("someuser")}</user-id>
 </options>)
(:
  returns "someuser", assuming "someuser" exists in the
  security database and the user running the eval request has the
  xdmp:login privilege.
:)

Example

let $s := "var h; var w; xdmp.log(h+' '+w);"
return
   xdmp:javascript-eval($s,("h","hello","w","world"))
(:
  logs "hello world" in ErrorLog.
:)

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.