XQuery/Registered Functions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

You want a list of all functions or all modules and their functions.

Method[edit | edit source]

There are two functions that we can use to get a list of functions in the current run-time system:

  util:registered-functions()
  util:registered-functions($module)

The first function returns all registered functions, the second returns all registered functions for a given module.

List all registered functions[edit | edit source]

The following XQuery creates a list of all the XQuery functions in alphabetical order. The output will depend on the modules configured in the installation.

xquery version "1.0";
import module namespace util = "http://exist-db.org/xquery/util"; 
<results>{
   for $function in util:registered-functions()
   order by $function
   return
     <function>{ $function}</function>
   }
    <total-count>{count(util:registered-functions())}</total-count>
</results>

Run

<results>
    <function>compression:gzip</function>
    <function>compression:tar</function>
    <function>compression:zip</function>
    <function>datetime:count-day-in-month</function>
    <function>datetime:date-for</function>
    ...


Note that if there is no namespace prefix, the function is an XPath library function. (or the math module which also appears without a prefix ??)

Listing all functions by module[edit | edit source]

A more useful format would be to list functions by module:

xquery version "1.0";
import module namespace util = "http://exist-db.org/xquery/util";
<results>{
   for $module in util:registered-modules()
   order by $module
   return
   <module>
      <module-uri>{$module}</module-uri>
      {let $functions := util:catch("*",util:registered-functions($module), ()) 
         (: added because one module (console ) has a missing namespace and throws an error - not sure why :)
       return 
        if (exists($functions)) 
        then
          for $function in $functions
          order by $function
          return
            <function>{$function}</function>
        else
          <error>Namespace undefined</error>
      }  
   </module>
   }
</results>

Run

Sample Output:

<results>
    <module>
        <module-uri>http://exist-db.org/xquery/compression</module-uri>
        <function>compression:gzip</function>
        <function>compression:tar</function>
        <function>compression:zip</function>
    </module>

    <module>
        <module-name>http://exist-db.org/xquery/datetime</module-name>
        <function>datetime:count-day-in-month</function>
        <function>datetime:date-for</function>

    ...