Running XQuery from Java applications using XQJ API - XQuery Processor for Java
The purpose of this article is how to use xquery/xpath functions like fn:pare-xml, fn:sum etc in XQuery using java applications.
This post shows the steps for running XQuery from java program using XQJ API - JSR 225: XQuery API for Java (XQJ).
XQJ allows a java application to connect to XML data sources
and query the data using XQuery.
This is a simple order processing use case where order total
was calculated based on product, price and order item quantity and discount was
calculated based on customer type.
Product details and customer details are stored in XML source file.
Customer type and discount values are stored in another XML source (config) file.
Order details will be sent as a string to the XQuery so that
fn:parse-xml (to convert string XML to structured form for processing) can be
used to get the order element for processing in XQuery.
XQuery supports modules where common functions can
be developed as modules and can be included in XQueries where ever they
are required.
The JAR files required for using the XQJ implementation from
Oracle XDK are:
xqjapi.jar, oxquery.jar, apache-xmlbeans.jar, xmlparserv2_sans_jaxp_services.jar
Your application must run with Java version 1.6.
Above jar files are
available from Oracle XQuery for Hadoop implementation
Download
Oracle XQuery for Hadoop 2.4.1 from Oracle.
Steps:
- Keep Jar files in JDeveloper class path:
- Orderdetails XQuery
- getOrder.xq XQuery: Usage of fn:parse-xml, fn:sum, fn:number, ora-fn:date-to-string-with-format, ora-fn:trim functions etc and also calling function getDiscount from module getDiscount.xq
- getDiscount.xq XQuery:
- Input XML: contains products and customer information
- Discount config XML
- Get Order Details class: XQJ API usage
Oracle XQuery
supports the
XQuery
1.0,
XPath
2.0 Functions and Operators and
XQuery
3.0 features
group
by clause
Standard
functions
fn:serialize
fn:parse-xml
etc.
Modules
support (calling xquery from another xquery)
The
XQuery works properly in OSB environment and even in Eclipse OEPE
fn-bea:inlinedXML
fn-bea:date-from-dateTime
Utility functions:
The utility module contains ora-fn functions for handling
strings and dates.
These functions are defined in XDK XQuery,
ora-fn - http://xmlns.oracle.com/xdk/xquery/function
String Functions
ora-fn:pad-left
ora-fn:pad-right
ora-fn:trim
ora-fn:trim-left
ora-fn:trim-right
Duration, Date, and Time Functions
ora-fn:date-from-string-with-format
ora-fn:date-to-string-with-format
ora-fn:dateTime-from-string-with-format
ora-fn:dateTime-to-string-with-format
ora-fn:time-from-string-with-format
ora-fn:time-to-string-with-format
Download the above article sample code here
Resources
http://docs.oracle.com/cd/E16655_01/appdev.121/e17604/adx_j_xqj.htm
http://technology.amis.nl/2011/07/30/running-xquery-from-java-applications-using-the-xqj-api-and-the-oracle-xdk-implementation/
http://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/