下面是一个通用的XSL样式表,其目的是将生成的数据转换为一个THML表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="resultset">
<h2 align="center">Default HTML Transform Result</h2>
<table border="1" align="center"><xsl:apply-templates/></table>
</xsl:template>
<xsl:template match="names">
<tr><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="name">
<td><xsl:apply-templates/></td>
</xsl:template>
<xsl:template match="row">
<tr><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="*">
<td><xsl:apply-templates/></td>
</xsl:template>
</xsl:stylesheet>
以下也是一个通用的XSL样式表,其目的是将生成的数据转换为一个无线传输标记语言(WML)表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="resultset">
<wml>
<card id="index" title="Default WML Transform Result">
<xsl:apply-templates/></card>
</wml>
</xsl:template>
<xsl:template match="names">
Names: <xsl:apply-templates/>
</xsl:template>
<xsl:template match="name">
<i><xsl:apply-templates/></i>
</xsl:template>
<xsl:template match="row">
<card><xsl:apply-templates/></card>
</xsl:template>
<xsl:template match="*">
<i><xsl:apply-templates/></i>
</xsl:template>
</xsl:stylesheet>
以下的通用XSL样式表是将生成数据转换为一个逗号分离值(CSVs)表,可以使用Excel直接读取:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="yes"/>
<xsl:template match="names">
<xsl:for-each select="*">
<xsl:if test="position() != last()"><xsl:value-of select="."/>,</xsl:if>
<xsl:if test="position() = last()"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="row">
<xsl:for-each select="*">
<xsl:if test="position() != last()"><xsl:value-of select="."/>,</xsl:if>
<xsl:if test="position() = last()"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
SQLMapper应用的一些思路
一个后台数据库应该包含定义网页用户界面窗体元素的表,使用几行代码可以在JSP页中检索这个表。也许你的网络服务器已经安装Oracle数据库,但却没有安装前台图形用户界面。现在你可以定义GUI的元素:
<%@ page language="java" contentType="text/html" import="sqlmapper.*, mywebapp.* %>
<%@ page errorPage="errorPage.jsp" %>
<html>
<!-- getUserArea.jsp executed on <%= (new java.util.Date()) %> -->
<!-- @Author: Charles Bell -->
<!-- @Version: April 22, 2003 -->
<head>
<title>Your company name - <%= dynamicTitle %></title>
</head>
<body background="<%= dynamicBackgroundImageFileName%>">
<%
WebAppUtilitymyWebAppUtility = new WebAppUtility();
String host = myWebAppUtility.getDatabaseHost();
String sid = myWebAppUtility.getDatabaseSID();
String userName = (String) session.getAttribute("validatedUserName");;
String password = myWebAppUtility.getDatabasePassord();
SQLMappermapper = new SQLMapper();
DataBaseHandlerdataBaseHandler=
new OracleDataBaseHandler(host, sid, userName, password);
mapper.setSQL("select * from FormDataElements");
mapper.setOutputType("html");
mapper.setDataBaseHandler(dataBaseHandler);
mapper.setXSLTranformStyleSheet("stylesheets/formdata.xsl");
out(mapper.map);
%>
<%@include file="footer.jsp" %>
</body>
</html>
使用这些技术,只要点击一个按钮,JSP页就可以弹出由数据库动态产生的最新报表。CSV输出可用于产生动态的Excel电子数据表。XML输出也可用于另一与自己的后台数据库通信的网络应用程序。
总结
我们已经讲述了建立DataBaseHandler通用行为的过程,并在一个抽象类实现了这一过程,而这一个类只需要几行Java代码就可以扩展成为一个自定义的、具体的数据库处理程序。SQLMapper类能够使用这一行为来连接一个关系数据库,执行一个SQL 查询,将数据转化为一个DOM文档对象。然后,使用一个XSL样式表将DOM对象转换为所需要的输出。通过高效、容易操作的方式提供所需要的输入,输出就可适用于任何应用程序的数据。
