Search code examples
xquerymarklogic

How to import common module namespaces in xquery


I have few module namespace xquery files which were used in multiple files. I want to have the namespaces in one common xquery file and import that file whereever I want to use.

Say for example,

I have process-lib.xqy, util-lib.xqy and query-lib.xqy. I used to import these in multiple files like following,

import module namespace util = "util" at "util-lib.xqy";
import module namespace process = "process" at "process-lib.xqy";
import module namespace query = "query" at "query-lib.xqy";

Now I tried to use them in one common file named as common-import.xqy and import this file in multiple files.

when I tried this approach,

import module namespace common-import= "common-import" at "common-import.xqy";

It throws exception as prefix util has no namespace binding.

How to achieve this?


Solution

  • This is not possible, at least not in the way you want to do it and rightfully so. The XQuery spec doesn't allow this:

    Module imports are not transitive—that is, importing a module provides access only to function and variable declarations contained directly in the imported module. For example, if module A imports module B, and module B imports module C, module A does not have access to the functions and variables declared in module C.
    

    This is a deliberate design decision. If you want to have access in this way you could write a wrapper function for each function you want to access, e.g. in your common-import.xqy file you could have:

    declare function common-import:test() { 
      util:test() 
    };
    

    But of course this can require a tremendous amount of wrapper functions. I would recommend you stick simply to inserting all required libraries. I see no benefit in doing otherwise.