1

I have a list of functions in a module that I want to call sequentially. At this time, I have to also keep a list of function names, then use fn:function-lookup to see that it exists, then xdmp:apply and xdmp:function to call the function.

Is there any way to get the sequence of functions listed in an imported module?

Alma Pellett
  • 307
  • 2
  • 8

2 Answers2

2

You could use xdmp:functions() to return the list of all in-scope functions, and then filter to select the methods bound to the namespace for the imported module:

import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
xdmp:functions() ! fn:function-name(.)[fn:namespace-uri-from-QName(.) = 'http://marklogic.com/xdmp/admin'] 
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Turns out this worked well, then I found out that the function using this is called inside a spawn-function. is there a way to add an "import module" to an xdmp:spawn-function? Without it, the xdmp:functions can only find the built-in functions. – Alma Pellett Feb 03 '21 at 23:41
  • 1
    Worked out a solution - wrap the function inside the spawn-function in an xdmp:eval. That allows us to reimport a library and redefine the scope for xdmp:functions. – Alma Pellett Feb 04 '21 at 17:36
0

Never tried myself, but I think you could figure it out with xdmp:functions(), and filter the full list by xdmp:function-module(). Mind thought, that it could very well be that order of functions is not maintained, in case that is important.

I also wonder why you are going through such lengths to do this. Sounds more like you are trying to run some tests or such.

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
  • I'm using the functions as an ordered list of nodes in a record. It could probably be done several other ways (like xslt), but this is where I am in this iteration of the code. – Alma Pellett Oct 26 '20 at 20:37
  • In that case, make sure xdmp:functions() returns them in correct order, or find a way to ensure this. The order in which they are returned is not defined, and could be unstable. – grtjn Oct 27 '20 at 20:18