ArticleslgStudy

science

XQuery API for Java

XQuery API for Java is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand XQuery API for Java rather than just read about it. In short: XQuery API for Java (XQJ) 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 (e.g. an XML database) while reducing or eliminating vendor lock in.

XQuery API for Java — main illustration
XQuery API for Java — illustration

Key takeaways

  • XQuery API for Java belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect XQuery API for Java to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of XQuery API for Java from memory before moving on to harder problems.

Reference excerpt

XQuery API for Java (XQJ) 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 (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. 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 (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 and may include full text searches. XQJ represents XQuery expressions using one of the following classes:

XQExpression – the expression is sent to the XQuery processor every time. XQPreparedExpression – the expression is cached and the execution path is pre-determined allowing it to be executed multiple times in an efficient manner. XQuery expressions return a result sequence of XDM items which in XQJ are represented through the XQResultSequence interface. The programmer can use an XQResultSequence 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 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.

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:

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.

Known implementations

Native XML databases The following is a list of Native XML Databases which are known to have XQuery API for Java implementations.

MarkLogic eXist BaseX Sedna Oracle XDB Tamino TigerLogic

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.

Oracle DB (Not XDB) IBM Db2 Microsoft SQL Server Sybase ASE Informix MySQL PostgreSQL

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

Saxon XSLT and XQuery processor Zorba MXQuery Oracle XQuery Processor

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

References

External links Javadoc for XQJ XQJ Tutorial Building Bridges from Java to XQuery, Charles Foster. XML Prague 2012 (Prezi Presentation) Java Integration of XQuery, Hans-Jürgen Rennau. Balisage 2010 Orbeon Forms using XQJ Archived 22 November 2012 at the Wayback Machine Spring Integration XQuery Support XQS: XQuery for Scala (Sits on top of XQJ) IntelliJ XQuery Support plugin

Illustrations

XQuery API for Java: General architecture of how an XQJ driver is used to communicate with an XML Database from Java Applications.
General architecture of how an XQJ driver is used to communicate with an XML Database from Java Applications.

Worked examples

Example 1 — a first encounter with XQuery API for Java

Start with the simplest possible case. Write down what XQuery API for Java claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to XQuery API for Java before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about XQuery API for Java ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of XQuery API for Java

In research
XQuery API for Java appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses XQuery API for Java in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
XQuery API for Java is common in secondary-school and first-year university syllabi. It links to neighbouring topics Database APIs, Java API for XML, Java APIs, so understanding it makes those chapters shorter.
In everyday life
Look for XQuery API for Java outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “XQuery API for Java” →

How to study XQuery API for Java in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what XQuery API for Java means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain XQuery API for Java out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is XQuery API for Java in simple terms?

XQuery API for Java (XQJ) 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 (e.g. an XML database) while reducing or eliminating vendor lock in.

Why does XQuery API for Java matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study XQuery API for Java?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on XQuery API for Java.

Tags

  • Database APIs
  • Java API for XML
  • Java APIs
  • Java specification requests
  • XML data access

Keep exploring