Although at the lowest level, xdmp: eval is really what happens, the cleanest option that is probably the easiest to write inline is xdmp: invoke-function - and it might even be better to use an anonymous function inside it. This combination allows the natural use of existing variables. If you want to go further, then look at xdmp: apply (to add extra flexibility)
In addition, in MarkLogic 8, there is a new type of transaction called update-auto-commit, which also makes it nice and clean to call the inline function, waiting for the results (without appearing) and have it in its own transaction. Used correctly, then the update / insert results are available even in the calling code.
The code example below uses cts: search for another database and naturally uses variables in the main code:
xquery version "1.0-ml"; declare namespace html = "http://www.w3.org/1999/xhtml"; let $query := cts:word-query("foo") let $start := 1 let $end := 3 let $database-name := "your-other-database-name-here" return xdmp:invoke-function( function() { cts:search(doc(), $query)[$start to $end] }, <options xmlns="xdmp:eval"> <database>{xdmp:database($database-name)}</database> </options>)
source share