Qexo - The GNU Kawa implementation of XQuery

Qexo is a partial implementation of the XML Query language. It achieves high performance because a query is compiled down to Java bytecodes using the Kawa framework. Kawa also includes a proof-of-concept implementation of XSLT.

The Qexo mailing list is has address qexo-general@gnu.org. To subscribe, unsubscribe, or view the archives use the information page.

Benefits

Documentation

You can read a short introduction to XQuery;
an article on the XQuery Data Model and Types;
and a longer article on generating XML and HTML using XQuery.

You can read here for more on running Qexo.
Here is an article debugging and finding errors in Qexo programs.

If you're interested in servlets and web applications, you can read an article showing how to write and install a trivial web application, or an article with an older but useful information.

Examples

Data conversion

Here is a program for converting a table:

let $newline := "
",
$result := (document("tab.xml")/result)
  return
    (<table>
{for $x in ($result/row)
      return (<tr>{
        for $y in ($x/fld1) return (<td><b>{children($y)}</b></td>),
        for $y in ($x/fld2) return (<td>{list(100,children($y))}</td>)}</tr>,
        $newline)
}</table>,$newline)

This will convert an input table like this:

<result>
<row>
<fld1>a1</fld1>
<fld2>12</fld2>
</row>
<row>
<fld1>b1</fld1>
<fld2>22</fld2>
</row>
</result>
yielding an output table like this:
<table>
<tr><td><b>a1</b></td><td><list>100 12</list></td></tr>
<tr><td><b>b1</b></td><td><list>100 22</list></td></tr>
</table>

Formatted table generation

The following program generates a 10x10 multiplication table:

<table>{
  for $y in 1 to 10 return (
    <tr>{
      for $x in 1 to 10 return
        let $bg:=(if($x mod 2 + $y mod 2 <= 0) then "lightgreen"
                  else if ($y mod 2 <= 0) then "yellow"
                  else if ($x mod 2 <= 0) then "lightblue"
                  else "white"),
            $prod:=$x*$y
          return <td align="right" bgcolor="{$bg}">{
            if ($x > 1 and $y > 1) then $prod else <b>{$prod}</b>}</td>
    }</tr>,
    "
")
}</table>,"
"

This is the result:
12345678910
2468101214161820
36912151821242730
481216202428323640
5101520253035404550
6121824303642485460
7142128354249566370
8162432404856647280
9182736455463728190
102030405060708090100

Defining a function

Here is an example of defining and using a recursive function:

define function descendent-or-self ($x) {
  $x, for $z in children($x) return descendent-or-self($z) }
descendent-or-self (<a>text1<b>text2</b></a>)
The result is: <a>text1<b>text2</b></a>text1<b>text2</b>text2

Photo album

I re-wrote from XML to XQuery my scripts for organizing my digital photos. The new shell script which calls pictures.xql is more readable, has similar size, yet provide more functionality. Here is an an example of the output (optimized for Mozilla).

Demo

You can try out Qexo without downloading it by using the XQuery sandbox hosted at cocoonhive.org. (Thanks to Ivelin Ivanov for setting this up.)

How to get and install Qexo

Qexo is part of Kawa. The easiest way to try it out it to download a runnable jar. Alternatively, you can get the source code. Both are available from the Kawa ftp site http://ftp.gnu.org/pub/gnu/kawa/ or a mirror However, XQuery is new and constantly changing, so you might prefer to get Kawa from the CVS repository, as described in the Kawa manual.

Usage

You run Kawa-XQuery just the way you run Kawa, except you need to specify the --xquery flag to specify the language. (By default Kawa expects Scheme source code.)

For example you can type in xquery expression directly at the command line. If the end of a the line occurs where the seen input is a valid XQuery expression, then that expression will be treated as a complete expression. (However, when parsing a file the entire file is parsed.)

$ java -jar kawa-1.8.jar --xquery
xquery[1]: for $x in (3,4,5) return <a>{$x}</a>
<a>3</a><a>4</a><a>5</a>
xquery[2]: string-value(<a>3+5 is {3+5}</a>)
 3+5 is 8
xquery[3]:

You can also compile an XQuery program to a Java application:

$ java -jar kawa-1.8.jar --xquery --main -C prog.xql
$ java -cp .:kawa-1.8.jar prog

You can read here for more on running Qexo.

Generating and Running Servlets

You can compile an XQuery program to a Servlet. Here is a simple web application example. This article has addition but older information. The servlet can be run in a servlet-aware web server, or as a CGI script.

License

Kawa-XQuery is available under the Kawa licence, which is the GNU General Public License with some extra options.

Status

This page summaries what works, relative to the most recent XQuery specification documents.
Per Bothner
Last modified: Thu Feb 3 22:11:55 PST 2005