XQuery API for Java


XQuery API for Java refers to the common Java API for the W3C XQuery 1.0 specification.
The XQJ API enables Java programmers to execute XQuery against an XML data source while reducing or eliminating vendor lock in.
The XQJ API provides Java developers with an interface to the XQuery Data Model. Its design is similar to the JDBC API which has a client/server feel and as such lends itself well to Server-based XML Databases and less well to client-side XQuery processors, although the "connection" part is a very minor part of the entire API. Users of the XQJ API can bind Java values to XQuery expressions, preventing code injection attacks. Also, multiple XQuery expressions can be executed as part of an atomic transaction.

History and implementation

The XQuery API for Java was developed at the Java Community Process as JSR 225. It had some big technology backers such as Oracle, IBM, BEA Systems, Software AG, Intel, Nokia and DataDirect.
Version 1.0 of the XQuery API for Java Specification was released on June 24, 2009, along with JavaDocs, a reference implementation and a TCK which implementing vendors must conform to.
The XQJ classes are contained in the Java package
There is no activity to create a version of XQJ that provides support for XQuery 3.0 or 3.1, for example by providing Java bindings for additions to the data model such as functions, arrays, or maps.

Functionality

XQJ allows multiple implementations to exist and be used by the same application.
XQJ connections support creating and executing XQuery expressions. Expressions may be updating and may include full text searches. XQJ represents XQuery expressions using one of the following classes:
XQuery expressions return a result sequence of XDM items which in XQJ are represented through the interface. The programmer can use an to walk over individual XDM items in the result sequence. Each item in the sequence has XDM type information associated with it, such as its node type e.g. element, document-node or an XDM atomic type such as xs:string, xs:integer or xs:dateTime. XDM type information in XQJ can be retrieved via the interface.
Atomic XQuery items can be easily cast to Java primitives via methods such as and . Also XQuery items and sequences can be serialized to DOM, SAX, StAX and the generic IO and classes.

Examples

Basic Example

The following example illustrates creating a connection to an XML Database, submitting an XQuery expression, then processing the results in Java. Once all of the results have been processed, the connection is closed to free up all resources associated with it.

// Create a new connection to an XML database
XQConnection conn = vendorDataSource.getConnection;
XQExpression expr = conn.createExpression; // Create a reusable XQuery Expression object
XQResultSequence result = expr.executeQuery//item " +
"return fn:data; // execute an XQuery expression
// Process the result sequence iteratively
while )
// Free all resources created by the connection
conn.close;

Binding a value to an external variable

The following example illustrates how a Java value can be bound to an external variable in an XQuery expression.
Assume that the connection conn already exists

XQExpression expr = conn.createExpression;
// The XQuery expression to be executed
String es = "declare variable $x as xs:integer external;" +
" for $n in fn:collection//item" +
" where $n/price <= $x" +
" return fn:data";
// Bind a value to an external variable with the QName x
expr.bindInt;
// Execute the XQuery expression
XQResultSequence result = expr.executeQuery;
// Process the result iteratively
while )

Default data type mapping

Mapping between Java and XQuery data types is largely flexible, however the XQJ 1.0 specification does have default mapping rules mapping data types when they are not specified by the user. These mapping rules bear great similarities to the mapping rules found in JAXB.
The following table illustrates the default mapping rules for when binding Java values to external variables in XQuery expressions.
Java DatatypeDefault XQuery Data Type
booleanxs:boolean
bytexs:byte
bytexs:hexBinary
doublexs:double
floatxs:float
intxs:int
longxs:long
shortxs:short
xs:boolean
xs:byte
xs:float
xs:double
xs:int
xs:long
xs:short
xs:string
xs:decimal
xs:integer
xs:dayTimeDuration if the Duration Object's state is xs:dayTimeDuration
xs:yearMonthDuration if the Duration Object's state is xs:yearMonthDuration
xs:duration if the Duration Object's state is xs:duration
xs:date if the XMLGregorianCalendar Object's state is xs:date
xs:dateTime if the XMLGregorianCalendar Object's state is xs:dateTime
xs:gDay if the XMLGregorianCalendar Object's state is xs:gDay
xs:gMonth if the XMLGregorianCalendar Object's state is xs:gMonth
xs:gMonthDay if the XMLGregorianCalendar Object's state is xs:gMonthDay
xs:gYear if the XMLGregorianCalendar Object's state is xs:gYear
xs:gYearMonth if the XMLGregorianCalendar Object's state is xs:gYearMonth
xs:time if the XMLGregorianCalendar Object's state is xs:time
xs:QName
document-node
document-node
element
attribute
comment
processing-instruction
text

Known implementations

Native XML databases

The following is a list of Native XML Databases which are known to have XQuery API for Java implementations.
provide XQJ adapters for relational databases, by translating XQuery code into SQL on the fly, then converting SQL result sets into a format suitable for XQJ to process further. The following is a couple of known implementations.
The following is a list of non-database XQuery processors which provide an XQuery API for Java interface.
The specification is marked as "Copyright © 2003, 2006 - 2009 Oracle. All rights reserved."
The specification contains two separate licenses: a "specification license" and a "reference implementation license".
The specification license allows free copying of the specification provided that copyright notices are retained; it also grants a license to create and distribute an implementation of the
specification provided that it fully implements the entire specification, that it does not modify or extend any interfaces, and that it passes the compatibility tests.
This provision has caused some controversy. Firstly, it is not universally accepted that implementing a published specification is something that requires a license. Secondly, the license does not meet the criteria to qualify as an open source license, because of the ban on making extensions and modifications. This has led some open source enthusiasts to challenge whether XQJ implementations can ever be considered truly open source.
The license for the reference implementation is a fairly conventional BSD-style open source license.