'''Main Module''' <code>hello-universe.xq</code>:
<syntaxhighlight pre lang="'xquery"'>
import module namespace m = 'http://basex.org/modules/hello' at 'hello-world.xqm';
m:hello("Universe")
</syntaxhighlightpre>
'''Library Module''' <code>hello-world.xqm</code> (in the same directory):
<syntaxhighlight pre lang="'xquery"'>
module namespace m = 'http://basex.org/modules/Hello';
declare function m:hello($world) {
'Hello ' || $world
};
</syntaxhighlightpre>
If no location is supplied, modules will be looked up in the repository. Repository modules are stored in the {{Code|repo}} directory, which resides in your [[Configuration#Home Directory|home directory]]. XQuery modules can be manually copied to the repository directory or installed and deleted via [[#Commands|commands]].
The following example calls a function from the FunctX module in the repository:
<syntaxhighlight pre lang="'xquery"'>
import module namespace functx = 'http://www.functx.com';
functx:capitalize-first('test')
</syntaxhighlightpre>
=Commands=
A module or package can be installed with {{Command|REPO INSTALL}}. The path to the file has to be given as a parameter:
REPO INSTALL httphttps://files.basex.org/modules/expath/functx-1.0.xar
REPO INSTALL hello-world.xqm
'''Example:'''
Installation (the original file will be copied to the {{Code|org/basex/modules/Hello}} sub-directory subdirectory of the repository):
REPO INSTALL httphttps://files.basex.org/modules/org/basex/modules/Hello/HelloWorld.xqm
Importing the repository module:
<syntaxhighlight pre lang="'xquery"'>
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</syntaxhighlightpre>
==Java==
Contents of the file {{Code|Hello.java}} (comments removed):
<syntaxhighlight pre lang="java">
package org.basex.modules;
public class Hello {
}
}
</syntaxhighlightpre>
Installation (the file will be copied to {{Code|org/basex/modules/Hello.jar}}):
XQuery file <code>[https://files.basex.org/modules/org/basex/modules/Hello/HelloUniverse.xq HelloUniverse.xq]</code> (same as above):
<syntaxhighlight pre lang="'xquery"'>
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</syntaxhighlightpre>
After having installed the module, all of the following URIs can be used in XQuery to import this module or call its functions (see [[Java Bindings#URI Rewriting|URI Rewriting]] for more information):
A Java class may depend on additional libraries. The dependencies can be resolved by creating a fat JAR file, i.e., extracting all files of the library archives and producing a single, flat JAR package.
Another solution is to copy the libraries into a {{Code|lib}} directory of the JAR package. When the package is installed, the additional library archives will be extracted and copied to a hidden sub-directory subdirectory in the repository. If the package is deleted, the hidden sub-directory subdirectory will be removed as well.
; Examplary contents of {{Code|Image.jar}}
; Main Module {{Code|hello-universe.xq}}:
<syntaxhighlight pre lang="'xquery"'>
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</syntaxhighlightpre>
; Wrapper Module {{Code|Hello.xqm}}:
<syntaxhighlight pre lang="'xquery"'>
module namespace hello = 'http://basex.org/modules/Hello';
java:hello($world)
};
</syntaxhighlightpre>
; Java class {{Code|Hello.java}}:
<syntaxhighlight pre lang="java">
package org.basex.modules;
}
}
</syntaxhighlightpre>
If the JAR file is installed, {{Code|Combined}} will be displayed as type:
REPO INSTALL httphttps://files.basex.org/modules/org/basex/modules/Hello.jar
REPO LIST
=EXPath Packaging=
The [http://expath.org/spec/pkg EXPath specification] defines the structure of a .xar archive. The package contains at its root a package descriptor named <code>expath-pkg.xml</code>. This descriptor presents some meta data metadata about the package as well as the libraries which it contains and their dependencies on other libraries or processors.
==XQuery==
* Apart from the package descriptor <code>expath-pkg.xml</code>, the package has to contain a descriptor file at its root, defining the included jars and the binary names of their public classes. It must be named <code>basex.xml</code> and must conform to the following structure:
<syntaxhighlight pre lang="xml">
<package xmlns="http://expath.org/ns/pkg">
<jar>...</jar>
....
</package>
</syntaxhighlightpre>
* The jar file itself along with an XQuery file defining wrapper functions around the java methods has to reside in the module directory. The following example illustrates how java methods are wrapped with XQuery functions:
'''Example:'''<br>Suppose we have a simple class <code>Printer</code> having just one public method <code>print()</code>:
Suppose we have a simple class <syntaxhighlight code>Printer</code> having just one public method <code>print()</code>: <pre lang="java">
package test;
}
}
</syntaxhighlightpre>
We want to extend BaseX with this class and use its method. In order to make this possible we have to define an XQuery function which wraps the <code>print</code> method of our class. This can be done in the following way:
<syntaxhighlight pre lang="'xquery"'>
import module namespace j="http://basex.org/lib/testJar";
return p:print($printer, $str)
};
</syntaxhighlightpre>
As it can be seen, the class {{Code|Printer}} is declared with its binary name as a namespace prefixed with "java" and the XQuery function is implemented using the [http://docs.basex.org/wiki/Java_Bindings Java Bindings] offered by BaseX.