Embedding XQuery in Java

XQuery is a very powerful language. It can be very useful when you want to do some XML processing in Java.
Let’s say you want to create an XML document based on some other XML data. Given something like this:

    Fred Jones
    <address location="home">
      900 Aurora Ave.
    <address location="work">
      2011 152nd Avenue NE
    <phone location="work">(425)555-5665
    <phone location="home">(206)555-5555
    <phone location="mobile">(206)555-4321

You want to produce employees’ names:

  Fred Jones

In XQuery it’s just as easy as:

  {for $name in employees/employee/name/text() return {$name}}

The most interesting advantage for XQuery over various other methods for generating XML is that XQuery operates natively on XML.
There are tools like JaxB, XmlBeans, which enable strongly typed XML building directly from Java code. But using such approach often requires a lot of Java code to be written, which is not really necessary.
There is also a possibility to use XPath. However it’s an inferior solution to XQuery, because XPath doesn’t provide a way for building XML documents. It’s designed only for nodes selection. On the other hand, XQuery extends XPath, so it supports every construct that XPath does.
Another way to do such processing in Java is to use XSLT. It’s the closest approach to XQuery. But the problem with XSLT is that it has its language constructs, like ‘for’ expressed as XML elements. This makes writing XSLT code much more difficult that XQuery.
XQuery can be seen as a native template language for XML processing.
So the question is: how to evaluate XQuery expressions the best way from Java?
There are some Open Source implementations of XQuery for Java. One of them is inside XmlBeans. However in my opinion the best way is to use Saxon. It’s the most mature project for XQuery processing and it’s targetted directly for doing that.
However Saxon might be a bit difficult to use directly. At least digging a few interesting features from it took me some time.
So I decided to write a simple class for interfacing Saxon and to provide a few interesting examples of how to use it. That’s how xquery4j was born. You can download it from github http://github.com/rafalrusin/xquery4j.
In xquery4j, you can execute XQuery expressions from Java in a simple way:

XQueryEvaluator evaluator = new XQueryEvaluator();
Long result = (Long) evaluator.evaluateExpression("5+5", null).get(0);
Assert.assertEquals(new Long(10), result);

It’s possible to bind variables from Java objects, the easy way:

evaluator.bindVariable(QName.valueOf("myVar"), 123);

Sometimes it’s also useful to declare Java methods and bind them for XQuery expressions. This is also very simple to do with xquery4j:

public static class MyFunctions {
        public static String myHello(String arg) {
            TestEvaluator te = (TestEvaluator) XQueryEvaluator.contextObjectTL.get();
            return "hello(" + arg + te.id + ")";

XQueryEvaluator evaluator = new XQueryEvaluator();
evaluator.declareJavaClass(“http://my.org/employees”, MyFunctions.class);
This code sets a context object to ‘this’ and binds all static methods from MyFunctions class to XQuery expressions. So during myHello execution from XQuery, you can easily operate on Java variables from bound context – ‘id’ in this case.
Here’s a way of invoking such bound myHello method from XQuery:

declare namespace my = 'http://my.org/employees'; my:myHello("hello")

xquery4j code contains unit tests, which include examples above.
You can run them by:

mvn package

Feel free to give some feedback on using it.