From BaseX Documentation

Jump to: navigation, search

This article is part of the XQuery Portal. It describes how external XQuery modules and Java code can be installed in the XQuery module repository, and how new packages are built and deployed.



One of the things that makes languages successful is the availability of external libraries. As XQuery comes with only 150 pre-defined functions, which cannot meet all requirements, additional library modules exist – such as FunctX – which extend the language with new features.

BaseX offers the following mechanisms to make external modules accessible to the XQuery processor:

  1. The internal Packaging mechanism will install single XQuery and JAR modules in the repository.
  2. The EXPath Packaging system provides a generic mechanism for adding XQuery modules to query processors. A package is defined as a .xar archive, which encapsulates one or more extension libraries.

Accessing Modules

Library modules can be imported with the import module statement, followed by a freely choosable prefix and the namespace of the target module. The specified location may be absolute or relative; in the latter case, it is resolved against the location (i.e., static base URI) of the calling module. Import module statements must be placed at the beginning of a module:

Main Module hello-universe.xq:

import module namespace m = '' at 'hello-world.xqm';

Library Module hello-world.xqm (in the same directory):

module namespace m = '';
declare function m:hello($world) {
  'Hello ' || $world

If no location is supplied, modules will be looked up in the repository. Repository modules are stored in the repo directory, which resides in your home directory. XQuery modules can be manually copied to the repository directory or installed and deleted via commands.

The following example calls a function from the FunctX module in the repository:

import module namespace functx = '';


There are various ways to organize your packages:

You can even manually add and remove packages in the repository directory; all changes will automatically be detected by BaseX.


A module or package can be installed with REPO INSTALL. The path to the file has to be given as a parameter:

REPO INSTALL hello-world.xqm

The installation will only succeed if the specified file conforms to the constraints described below. If you know that your input is valid, you may as well copy the files directly to the repository directory, or edit its contents in the repository without deleting and reinstalling them.


All currently installed packages can be listed with REPO LIST. The names of all packages are listed, along with their version, their package type, and the repository path:

Name                   Version  Type      Path
-----------------------------------------------------------------  1.0      EXPath


A package can be deleted with REPO DELETE and an additional argument, containing its name or the name suffixed with a hyphen and the package version:




If an XQuery file is specified as input for the install command, it will be parsed as XQuery library module. If the file can successfully be parsed, the module URI will be rewritten to a file path and attached with the .xqm file suffix, and the original file will possibly be renamed and copied to that path into the repository.


Installation (the original file will be copied to the org/basex/modules/Hello sub-directory of the repository):


Importing the repository module:

import module namespace m = '';


For general notes on importing Java classes, please read the Java Bindings article on Module Imports.

Java archives (JARs) may contain one or more class files. One of them will be chosen as main class, which must be specified in a Main-Class entry in the manifest file (META-INF/MANIFEST.MF). This fully qualified Java class name will be rewritten to a file path by replacing the dots with slashes and attaching the .jar file suffix, and the original file will be renamed and copied to that path into the repository.

If the class will be imported in the prolog of the XQuery module, an instance of it will be created, and its public functions can then be addressed from XQuery. A class may extend the QueryModule class to get access to the current query context and to be enriched by some helpful annotations (see Annotations).


Structure of the HelloWorld.jar archive:


Contents of the file (the whitespaces are obligatory):

Manifest-Version: 1.0
Main-Class: org.basex.modules.Hello

Contents of the file (comments removed):

package org.basex.modules;
public class Hello {
  public String hello(final String world) {
    return "Hello " + world;

Installation (the file will be copied to org/basex/modules/Hello.jar):

REPO INSTALL HelloWorld.jar

XQuery file HelloUniverse.xq (same as above):

import module namespace m = '';

After having installed the module, all of the following URIs can be used in XQuery to import this module or call its functions (see URI Rewriting for more information):

Additional Libraries

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 lib directory of the JAR package. If the package will be installed, the additional library archives will be extracted and copied to a hidden sub-directory in the repository. If the package will be deleted, the hidden sub-directory will be removed as well.

Examplary contents of Image.jar
Directory structure of the repository directory after installing the package


It makes sense to combine the advantages of XQuery and Java packages:

XQuery and Java can be combined as follows:

If the resulting JAR file is installed, the embedded XQuery module will be extracted, and will be called first if the module will be imported.

Main Module hello-universe.xq
import module namespace m = '';
Wrapper Module Hello.xqm
module namespace hello = '';

(: Import JAR file :)
import module namespace java = 'java:org.basex.modules.Hello';

 : Say hello to someone.
 : @param  $world  the one to be greeted
 : @return welcome string
declare function hello:hello(
  $world  as xs:string
) as xs:string {
Java class
package org.basex.modules;

public class Hello {
  public String hello(final String world) {
    return "Hello " + world;

If the JAR file is installed, Combined will be displayed as type:


Name                     Version  Type      Path
org.basex.modules.Hello  -        Combined  org/basex/modules/Hello.xqm

EXPath Packaging

The EXPath specification defines how the structure of a .xar archive shall look like. The package contains at its root a package descriptor named expath-pkg.xml. This descriptor presents some meta data about the package as well as the libraries which it contains and their dependencies on other libraries or processors.


Apart from the package descriptor, a .xar archive contains a directory which includes the actual XQuery modules. For example, the FunctX XAR archive is packaged as follows:



If you want to package an EXPath archive with Java code, some additional requirements have to be fulfilled:

<package xmlns="">

Suppose we have a simple class Printer having just one public method print():

package test;

public final class Printer {
  public String print(final String s) {
    return new Writer(s).write();

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 print method of our class. This can be done in the following way:

import module namespace j="";

declare namespace p="java:test.Printer";

declare function j:print($str as xs:string) as xs:string {
  let $printer := p:new()
  return p:print($printer, $str)

As it can be seen, the class Printer is declared with its binary name as a namespace prefixed with "java" and the XQuery function is implemented using the Java Bindings offered by BaseX.

On our file server, you can find some example libraries packaged as XML archives (xar files). You can use them to try our packaging API or just as a reference for creating your own packages.


Importing XQuery modules that are located in the repository is just as fast as importing any other modules. Modules that are imported several times in a project will only be compiled once.

Imported Java archives will be dynamically added to the classpath and unregistered after query execution. This requires some constant overhead and may lead to unexpected effects in scenarios with highly concurrent read operations. If you want to get optimal performance, it is recommendable to move your JAR files into the lib/custom directory of BaseX. This way, the archive will be added to the classpath if BaseX is started. If you have installed a Combined Package, you can simply keep your XQuery module in the repository, and the Java classes will be automatically detected.


Version 9.0
Version 7.2.1
Version 7.1
Version 7.0
Personal tools