XQuery API for Java

Last updated

XQJ
Developer(s) Java Community Process
Stable release
1.0 / 24 June 2009 (2009-06-24)
Type Data Access API
Website JSR 225: XQuery API for Java
General architecture of how an XQJ driver is used to communicate with an XML Database from Java Applications. XQJ-Architecture.svg
General architecture of how an XQJ driver is used to communicate with an XML Database from Java Applications.

XQuery API for Java (XQJ) refers to the common Java API for the W3C XQuery 1.0 specification.

Contents

The XQJ API enables Java programmers to execute XQuery against an XML data source (e.g. an XML database) while reducing or eliminating vendor lock in.

The XQJ API provides Java developers with an interface to the XQuery Data Model. [1] 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. [2] 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, [3] [4] [5] [6] IBM, [5] [6] BEA Systems, [7] Software AG, [8] Intel, Nokia and DataDirect. [4]

Version 1.0 of the XQuery API for Java Specification was released on June 24, 2009, [9] along with JavaDocs, a reference implementation and a TCK (Technology Compatibility Kit) which implementing vendors must conform to.

The XQJ classes are contained in the Java package javax.xml.xquery

There is no (visible) 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 [10] and may include full text searches. [11] XQJ represents XQuery expressions using one of the following classes:

XQuery expressions return a result sequence of XDM [1] items which in XQJ are represented through the XQResultSequence interface. The programmer can use an XQResultSequence to walk over individual XDM [1] items in the result sequence. Each item in the sequence has XDM [1] 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 XQItemType interface.

Atomic XQuery items can be easily cast to Java primitives via XQItemAccessor methods such as getByte() and getFloat() . Also XQuery items and sequences can be serialized to DOM Node , SAX ContentHandler , StAX XMLStreamReader and the generic IO Reader and InputStream 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 databaseXQConnectionconn=vendorDataSource.getConnection("myUser","myPassword");XQExpressionexpr=conn.createExpression();// Create a reusable XQuery Expression objectXQResultSequenceresult=expr.executeQuery("for $n in fn:collection('catalog')//item "+"return fn:data($n/name)");// execute an XQuery expression// Process the result sequence iterativelywhile(result.next()){// Print the current item in the sequenceSystem.out.println("Product name: "+result.getItemAsString(null));}// Free all resources created by the connectionconn.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:

XQExpressionexpr=conn.createExpression();// The XQuery expression to be executedStringes="declare variable $x as xs:integer external;"+" for $n in fn:collection('catalog')//item"+" where $n/price <= $x"+" return fn:data($n/name)";// Bind a value (21) to an external variable with the QName xexpr.bindInt(newQName("x"),21,null);// Execute the XQuery expressionXQResultSequenceresult=expr.executeQuery(es);// Process the result (sequence) iterativelywhile(result.next()){// Process the result ...}

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.

Default conversion rules when mapping from Java data types to XQuery data types
Java DatatypeDefault XQuery Data Type(s)
booleanxs:boolean
bytexs:byte
byte[]xs:hexBinary
doublexs:double
floatxs:float
intxs:int
longxs:long
shortxs:short
Boolean xs:boolean
Byte xs:byte
Float xs:float
Double xs:double
Integer xs:int
Long xs:long
Short xs:short
String xs:string
BigDecimal xs:decimal
BigInteger xs:integer
Duratio 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
XMLGregorianCalendar 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
QName xs:QName
Document document-node(element(*, xs:untyped))
DocumentFragment document-node(element(*, xs:untyped))
Element element(*, xs:untyped)
Attr attribute(*, xs:untypedAtomic)
Comment comment()
ProcessingInstruction processing-instruction()
Text 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.

Relational databases

DataDirect 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.

Non-database implementations

The following is a list of non-database XQuery processors which provide an XQuery API for Java interface (typically allowing query against documents parsed from XML in filestore, and held in memory as DOM or similar trees).

License

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 (that is, that copyright law would disallow this in the absence of a license). [20] [21] Secondly, the license does not meet the criteria to qualify as an open source license (see Open Source Definition), 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.

Related Research Articles

<span class="mw-page-title-main">Object database</span> Type of database management system

An object database or object-oriented database is a database management system in which information is represented in the form of objects as used in object-oriented programming. Object databases are different from relational databases which are table-oriented. A third type, object–relational databases, is a hybrid of both approaches.

Structured Query Language (SQL) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS). It is particularly useful in handling structured data, i.e., data incorporating relations among entities and variables.

<span class="mw-page-title-main">XML</span> Markup language by the W3C for encoding of data

Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The World Wide Web Consortium's XML 1.0 Specification of 1998 and several other related specifications—all of them free open standards—define XML.

XSLT is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG. Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.

Object–relational mapping in computer science is a programming technique for converting data between a relational database and the heap of an object-oriented programming language. This creates, in effect, a virtual object database that can be used from within the programming language.

eXist-db is an open source software project for NoSQL databases built on XML technology. It is classified as both a NoSQL document-oriented database system and a native XML database. Unlike most relational database management systems (RDBMS) and NoSQL databases, eXist-db provides XQuery and XSLT as its query and application programming languages.

Jakarta PersistenceAPI is a Jakarta EE application programming interface specification that describes the management of relational data in enterprise Java applications.

SQL/XML or XML-Related Specifications is part 14 of the Structured Query Language (SQL) specification. In addition to the traditional predefined SQL data types like NUMERIC, CHAR, TIMESTAMP, ... it introduces the predefined data type XML together with constructors, several routines, functions, and XML-to-SQL data type mappings to support manipulation and storage of XML in a SQL database.

Language Integrated Query is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007.

XPath is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values from the content of an XML document. Support for XPath exists in applications that support XML, such as web browsers, and many programming languages.

Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.

XQuery is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats. The language is developed by the XML Query working group of the W3C. The work is closely coordinated with the development of XSLT by the XSL Working Group; the two groups share responsibility for XPath, which is a subset of XQuery.

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

Effi is C++ application development framework.

In database management systems (DBMS), a prepared statement, parameterized statement, or parameterized query is a feature where the database pre-compiles SQL code and stores the results, separating it from data. Benefits of prepared statements are:

<span class="mw-page-title-main">XML transformation language</span> Type of programming language

An XML transformation language is a programming language designed specifically to transform an input XML document into an output document which satisfies some specific goal.

Zorba is an open source query processor written in C++, implementing

Stylus Studio is an integrated development environment (IDE) for the Extensible Markup Language (XML). It consists of a variety of tools and visual designers to edit and transform XML documents and legacy data such as electronic data interchange (EDI), comma-separated values (CSV) and relational data.

JSONiq is a query and functional programming language that is designed to declaratively query and transform collections of hierarchical and heterogeneous data in format of JSON, XML, as well as unstructured, textual data.

Oracle TopLink is a mapping and persistence framework for Java developers. TopLink is produced by Oracle and is a part of Oracle's OracleAS, WebLogic, and OC4J servers. It is an object-persistence and object-transformation framework. TopLink provides development tools and run-time functionalities that ease the development process and help increase functionality. Persistent object-oriented data is stored in relational databases which helps build high-performance applications. Storing data in either XML or relational databases is made possible by transforming it from object-oriented data.

References

  1. 1 2 3 4 XQuery 1.0 and XPath 2.0 Data Model (XDM)
  2. Binding Java Variables
  3. Querying XML: XQuery, XPath, and SQL/XML in context - Jim Melton and Stephen Buxton. ISBN   978-1558607118
  4. 1 2 XQJ - XQuery Java API is Completed, Marc Van Cappellen, Zhen Hua Liu, Jim Melton and Maxim Orgiyan Archived 28 July 2012 at the Wayback Machine
  5. 1 2 IBM and Oracle Submit XQuery API for Java (XQJ) Java Specification Request.
  6. 1 2 An Early Look at XQuery API for Java (XQJ) - Andrew Eisenberg, IBM and Jim Melton, Oracle Archived 28 July 2012 at the Wayback Machine
  7. The BEA Streaming XQuery Processor
  8. XQJ Interface for Tamino Native XML Database Archived 30 May 2013 at the Wayback Machine
  9. JSR-000225 XQuery API for Java (Final Release)
  10. XQuery Update Facility
  11. XQuery Full Text
  12. MarkLogic XQJ API
  13. eXist XQJ API
  14. BaseX XQJ API
  15. Sedna XQJ API
  16. Oracle XML DB Support for XQJ
  17. Software AG - Working with the CentraSite XQJ Interface
  18. Zorba 2.5 ships with a long awaited XQJ binding, 14 June 2012
  19. Oracle XML Developer's Kit (XDK) provides a standalone XQuery 1.0 processor for use by Java applications.
  20. "Open Standards" (PDF). Retrieved 7 September 2023.
  21. "Groklaw - Oracle v. Google - Understanding the Copyright Issue with API Specifications". www.groklaw.net. Archived from the original on 5 May 2012.