DataDirect XQuery to the Rescue

XQuery for Web Sites

Buy Now | Download | Learn More

Many web sites feature several sections whose content and even format is dynamically controlled by data stored in relational databases or XML documents, often using ASP or JSP. XQuery can easily be used to create such HTML pages directly, to create XML that in turn can be transformed into HTML (for example, using XSLT). In alternative, XQuery can be used to provide services returning XML meant to be consumed by AJAX applications.

XQuery as Java Servlets
Most web sites need to protect critical resources like databases from their outside users, ensuring that web clients cannot invoke code that runs in the web server or application server. Java Servlets, for instance, provide a level of indirection between data that is served by the web server and the underlying implementation and storage of such data. XQuery can be exposed by a Servlet, providing the high level data access required by a Web application, while protecting the underlying data from direct access from the outside world.

As an example; We want the end-user to be able to call a query named "portfolio", specifying the name of a user whose portfolio should be returned.

http://tagsalad.org/TomcatXQJExecute?query=portfolio&user=jonathan

On the middle tier, we need some Java code to call the appropriate query and bind the parameters to external variables. This code uses the XQJ API, which is the JDBC for XQuery. This code also sets up the connections for the query, which must be installed on the middle tier, where it is protected from the outside world.

// Set up the data source, connection, and expression objects
dataSource = new DDXQDataSource(new FileInputStream(configFile));
connection = dataSource.getConnection();
xqExpression = connection.createExpression();

// bind URL parameters to XQuery external variables      
Iterator i = parameters.entrySet().iterator();
while ( i.hasNext() ){
 Map.Entry entry = (Map.Entry)i.next();
 xqExpression.bindString(new QName((String)entry.getKey()),(String)entry.getValue());
} 
     
// Execute the query
xquerySourceFile = request.getParameter("query") + ".xquery";
xqSequence = xqExpression.executeQuery(new FileReader(xquerySourceFile));

XQueries that return HTML
In some applications, XQuery is used to return HTML directly. These applications use XQuery much like ASP or JSP, to generate HTML with dynamic content. Here is an XQuery that returns a portfolio in HTML.

declare variable $user external;

<html>
  <head>
    <title>Portfolio for {$user}</title>
  </head>
  <body>
    <h1>Portfolio for {$user}</h1>
    <table>
     <thead>
       <tr>
        <td>Ticker</td>
        <td>Shares</td>
       </tr>
     </thead>
          
     <tbody>
      {
       for $st in collection('stock.dbo.HOLDINGS')/HOLDINGS
       where xs:string($st/USERID) = xs:string($user)
       return
        <tr>
         <td>{ data($st/STOCKTICKER) }</td>
         <td>{ data($st/SHARES) }</td>
        </tr>
      }
     </tbody>          
  </table>
 </body>
</html>

This query can be invoked using a URL as discussed above.

XQueries that return XML for AJAX Applications or XSLT Transformation
Many applications create dynamic content for web pages by generating XML, then translating the XML to HTML using XSLT. Other applications use XML as input to JavaScript or Java on the client side, especially in AJAX applications. For instance, an application might use the following XML to represent a portfolio:

<portfolio>
  <user>Jonathan</user>
  <stock>
    <ticker>AMZN</ticker>

    <shares>3000.00</shares>
  </stock>
  <stock>
    <ticker>PRGS</ticker>
    <shares>23.00</shares>
  </stock>
</portfolio>

This XML can be generated by the following XQuery query, which uses relational data to create the XML.

declare variable $user as xs:string external;

<portfolio>
 <user>{ $user }</user>
 {
  for $h in collection('stock.ts.HOLDINGS')/HOLDINGS
  where $h/USERID eq $user
  return 
    <stock>
    <ticker>{ xs:string($h/STOCKTICKER) }</ticker>
    <shares>{ xs:string($h/SHARES) }</shares>    
    </stock>
 }
</portfolio>

This query can be invoked using a URL as discussed above.

Product Information

Quick Links

XQuery for EDI and Flat File Formats

DataDirect XQuery Product Fact Sheet

DataDirect XQuery FAQ


Copyright © 1993 - 2008. Progress Software Corporation. All rights reserved. | N. America: 800 876 3101 | World: +44 (0) 1753 218 930