Web Publishing

Web Publishing is made much easier by Data Integration Suite. Many web sites are created from database data, often using PHP, ASP or JSP. XQuery can be used to create dynamic HTML pages directly, to create XML to be transformed by XSLT to create HTML, or to create HTML to be consumed by AJAX applications.



XQuery on the Middle Tier

Most web sites need to protect critical resources like databases from their outside users, so they ensure that web clients cannot invoke code that runs in the web server or application server. For instance, Java servlets can be installed by placing them in a directory that is not accessible to the outside world, and can then be invoked using a URL. The same strategy can be used in XQuery. Suppose we want the 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. These applications use XQuery much like PHP, 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. In summary, XQuery can be used like PHP, JSP or ASP to publish data to the web.