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.
- 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
XPath 2.0 Functions and Operators and
XQuery 3.0 features
group by clause
Modules support (calling xquery from another xquery)
The XQuery works properly in OSB environment and even in Eclipse OEPE
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
Duration, Date, and Time Functions
Download the above article sample code here