![]()
You can use XQuery, an XML query language, to query XML data sources. In addition, you can use XQuery to query any type of data that can be virtually represented as XML, for example, relational databases or legacy data formats.
XQuery is a query language that was designed as a native XML query language. Because most types of data can be represented as XML, XQuery can also be used to query other types of data. For example, XQuery can be used to query relational data using an XML view of a relational database. This is important because many Internet applications need to integrate information from multiple sources, including data found in web messages, relational data, and various XML sources. XQuery was specifically designed for this kind of data integration.
For example, suppose your company is a financial institution that needs to produce reports of stock holdings for each client. A client requests a report with a Simple Object Access Protocol (SOAP) message, which is represented in XML. In most businesses, the stock holdings data is stored in multiple relational databases, such as Oracle, Microsoft SQL Server, MySQL, IBM DB2 and Sybase. XQuery can query both the SOAP message and the relational databases, creating a report in XML.
XQuery is based on the structure of XML and leverages that structure to make it possible to perform queries on any type of data that can be represented as XML, including relational data. In addition, XQuery API for Java (XQJ) lets your queries run in any environment that supports the J2EE platform.
XQuery 1.0 became a W3C Recommendation on 23 January 2007. The World Wide Web Consortium (W3C) is a standards body for the World Wide Web.
The XQuery API for Java (XQJ) is designed to support the XQuery language, just like the JDBC API supports the SQL query language. XQJ is based on the XQuery Data Model rather than the relational model. It allows a Java application to submit XQuery queries to data sources and process the results.
The XQJ standard (JSR 225) is being developed under the Java Community Process. For more information, see http://www.jcp.org/en/jsr/detail?id=225.
You need the XQuery API for Java (XQJ) to allow a Java application to submit XQuery queries to XML and relational data sources and process the results. XQJ allows a Java program to configure connections, issue XQuery queries, and obtain results. XQJ is to XQuery what JDBC is to SQL.
The XQJ standard (JSR 225) is being developed under the Java Community Process. For more information, see http://www.jcp.org/en/jsr/detail?id=225.
Many applications need to process relational data to generate complex reports, and they need these reports in XML. In addition, most commercial web pages use relational data, but it rarely looks like relational data when it is presented to the user. These sites combine information from many sources, creating complex web pages that are user friendly. For relational data, XQuery can be thought of as an XML-based report writing language. Similarly, SOAP messages are hierarchical XML, but the data found in them frequently comes from relational databases, and XML is also used in many print-based database publishing applications.
The benefits of Java are well-known. It lets you write applications that work on any operating system, and the J2EE platform provides robust support for XML, security, web services, and database connectivity. Embedding XQuery in Java applications can significantly reduce the amount of Java code that is needed, because XQuery often is easier to use and more efficient than Java for processing XML or querying relational data to create XML.
The XQuery for Java API (XQJ) is being developed to allow Java applications to use XQuery much like JDBC allows Java applications to use SQL. Using DataDirect XQuery, a Java application that embeds or references an XQuery query can run on most operating systems, can use data from most relational databases, and can access XML through the standard Java XML APIs (SAX and DOM, for example).
Web Services depend heavily on XML for both its definition language, Web Services Description Language (WSDL), and its messaging protocol, Simple Object Access Protocol (SOAP). XQuery provides a language that can interact with XML data and other types of data as long as they can be represented as XML. For example, XQuery can be used to access the content of messages or to construct new messages to be passed to a Web service. In addition, you often need data to process Web Services results. XQuery can process SOAP messages and create the XML needed for result messages.
The best way to explain how a single XQuery query can query both XML and relational data sources is to show an example. Suppose your company is a financial institution that needs to produce reports of stock holdings for each client. A client requests a report with a Simple Object Access Protocol (SOAP) message, which is represented in XML. In most businesses, the stock holdings data is stored in multiple relational databases, such as Oracle, Microsoft SQL Server, or DB2. XQuery can query both the SOAP message and the relational databases, creating a report in XML. The XQuery query is processed using an XQuery implementation, such as DataDirect XQuery.
The following example joins an XML document (our SOAP message) named "request.xml" to two relational database tables named "holdings" and "statistical". The request.xml XML document is joined to the "holdings" table by the userId attribute in the XML file and the userId column of the "holdings" table. The two tables are joined by the ticker column of the "statistical" table and the stockticker column of the "holdings" table.
let $request := doc('request.xml')/request
for $user in $request/performance/UserId
return
<portfolio UserId="{$user}">
{$request }
{
for $st in collection('holdings')/holdings,
$stats in collection('statistical')/statistical
where $st/userid = $user
and $stats/ticker = $st/stockticker
return
<stock>
{ $stats/companyname }
{ $st/stockticker }
{ $st/shares }
{ $stats/annualrevenues }
</stock>
}
</portfolio>
Result
The result of this query is an element named portfolio. The first child of this element contains the original request from request.xml. After that, the query provides the stock information for a given user, taken from two tables.
<portfolio UserId="Jonathan">
<request>
<performance>
<UserId>Jonathan</UserId>
<start>2003-01-01</start>
<end>2004-06-01</end>
</performance>
</request>
<stock>
<companyname>Amazon.com, Inc.</companyname>
<stockticker>AMZN</stockticker>
<shares>3000</shares>
<annualrevenues>7780</annualrevenues>
</stock>
<stock>
<companyname>eBay Inc.</companyname>
<stockticker>EBAY</stockticker>
<shares>4000</shares>
<annualrevenues>22600</annualrevenues>
</stock>
<stock>
<companyname>Int'l Business Machines C</companyname>
<stockticker>IBM</stockticker>
<shares>2500</shares>
<annualrevenues>128200</annualrevenues>
</stock>
<stock>
<companyname>Progress Software</companyname>
<stockticker>PRGS</stockticker>
<shares>23</shares>
<annualrevenues>493.4</annualrevenues>
</stock>
</portfolio>
DataDirect XQuery is the first embeddable component for XQuery that implements the XQuery API for Java (XQJ). It supports all major relational databases on any Java platform. DataDirect XQuery allows you to query XML, relational databases, or a combination of the two, integrating the results for XML-based data exchange, XML-driven Web sites, and other applications that require or leverage the power of XML.
DataDirect XQuery is designed for software developers and independent software vendors (ISVs) who need to manage heterogeneous data sources in XML applications.
The following architectural diagram shows the components of DataDirect XQuery and the flow involved in accessing both XML and relational data sources.

DataDirect XQuery is ideal for any company that needs to access and integrate data stored in XML and relational sources. Unlike other XQuery products, DataDirect XQuery provides the following benefits:
If you want to query and integrate both XML and relational data, DataDirect XQuery provides substantial benefits in performance over a free implementation such as Saxon:
Microsoft and Oracle support XQuery in their products. Unlike the XQuery implementations from database vendors, DataDirect XQuery is database and platform independent. Using DataDirect XQuery, you can write applications that work for all major databases as well as XML data sources.
Yes. DataDirect XQuery does not require its own server infrastructure, which makes it easy to embed into other commercial applications.
DataDirect XQuery is implemented in Java and runs on any J2SE 1.4 or higher Java platform.
DataDirect XQuery supports the following databases:
* Requires the MySQL Connector/J driver.
** Requires the PostgreSQL JDBC driver.
DataDirect XQuery can access XML data sources that have the following physical formats:
let $request := doc('file:///c:/request.xml')/request
...
let $request := doc('adapter:Base64:newline=crlf:
encoding=utf-8?file///w:/myfiles/base_to_xml.bin')/
request
...
DataDirect XQuery supports the XQuery 1.0 W3C Recommendation W3C Recommendation of 23 January 2007 located at: http://www.w3.org/TR/xquery/
Using a combination of DataDirect XQuery and JDBC offers a simple solution to working with both XML and relational data, and providing XQuery updates to relational data.
Configuring DataDirect XQuery for your environment is straightforward. Once installed, you configure a source configuration file to specify the connection information for XML and relational data sources that are used by XQuery queries. To make this configuration easy, DataDirect XQuery ships with an example source configuration file for each database that the product supports, so you can start with one of these example files and modify the values as needed.
Java applications use the XQuery for Java API (XQJ) to execute XQuery, which is analogous to using the JDBC API to execute SQL statements. The following sample Java code illustrates the basic steps that an application would perform to execute an XQuery expression using DataDirect XQuery.
...
// import the XQJ classes
import com.ddtek.xquery.*;
import com.ddtek.xquery.xqj.mediator.DDXQDataSource;
// establish a connection to a data source
DDXQDataSource ds = new DDXQDataSource();
ds.setJdbcUrl("jdbc:xquery:sqlserver://server1:1433;
databaseName=stocks");
XQConnection conn = ds.getConnection("myuserid", "mypswd");
// create an expression object that is used to execute a query
XQExpression expr = conn.createExpression();
// the query
String es = "for $h in collection('holdings')/holdings " +
"where $h/stockticker='AMZN' " +
"return $h";
// execute the query
XQResultSequence result = expr.executeQuery(es);
System.out.println(result.getSequenceAsString());
// free all resources
result.close();
expr.close();
conn.close();
Additional XQJ examples covering different scenarios are shipped with DataDirect XQuery.
Information about DataDirect XQuery can be found at http://www.datadirect.com/products/xquery/index.ssp.