Saturday, January 16, 2010

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:
    <name>Fred Jones</name>
    <address location="home">
      <street>900 Aurora Ave.</street>
    <address location="work">
      <street>2011 152nd Avenue NE</street>
    <phone location="work">(425)555-5665</phone>
    <phone location="home">(206)555-5555</phone>
    <phone location="mobile">(206)555-4321</phone>

You want to produce employees' names:
  <name>Fred Jones</name>

In XQuery it's just as easy as:
  {for $name in employees/employee/name/text() return <name>{$name}</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
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 + + ")";

    XQueryEvaluator evaluator = new XQueryEvaluator();
    evaluator.declareJavaClass("", 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 = ''; 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.


  1. hmm...I must appreciate you for the post you have shared.i really like it.thank you for sharing:)
    Dissertation Writing Services

  2. i really like your post .. i would like to bookmark your site for my future needs :)
    dissertation writing service

  3. Your website is amazing. It has a lot of informative stuff. I have become your fan. I will tell my friends about your website. I praise your work. Keep it up.
    thesis research paper

  4. I would like to say that your post is wonderful. We can learn many things from it. You have done a good work. Thanks for posting.
    writing acknowledgements for dissertation

  5. This comment has been removed by the author.