快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

JSP结合XML+XSLT将输出转变为HTML格式

我们知道 XML+XSLT就可以直接输出到支持XML的浏览器上,如IE 5.0以上,但是,我们还要考虑到有不少浏览器不直接支持XML,在这种情况下,我们需要在服务器上进行转变成html输出到浏览器,这种临时过渡办法恐怕要在一段时间内一直要使用.   使用Jsp 加上tablib标识库,我们可以完成这种转变。

  闻名open source项目组jakarta.apache.org推出的系列标识库中,就有这个功能的tanglib:http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

  按照jakarta配置方式,有点繁琐,需要修改或定义Web.xml,本人经过摸索,使用下列相称简朴的办法,就可以使Jsp能成功运行XSL这个标识库了。

  xsl标识库有三个要害包:
   xerces.jar 可以在http://xml.apache.org/中得到
   xalan.jar 可以在http://xml.apache.org/中得到
   xsl.jar 从http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html得到

  1.将这三个包放置到Tomcat的common/lib目录下,或者直接放入Classpath环境中。

  2.在JSP中调用标识库:

  原来Jakarta推荐方式是:


<%@taglib uri="http://jakarta.apache.org/taglibs/xsl-1.0" prefix="xsl" %>

  这就需要在/WEB-INF/web.xml下定义一下http://jakarta.apache.org/taglibs/xsl-1.0指向。如:


<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/xsl-1.0</taglib-uri>
<taglib-location>/WEB-INF/xsl.tld</taglib-location>
</taglib>

  这种做法虽然很标准,但是,假如你的容器一直使用tomcat,就完全不必了。

  我们的做法是:


<%@taglib uri="xsl.jar" prefix="xsl" %>

  我们以Jakarta的XSL taglib附带的Apply.jsp为例,正好了解一下Jsp XML XSLT三者之间的关系:

  Apply.jsp


<%@taglib uri="xsl.jar" prefix="xsl" %>
<html>
<head>
<title>Employee List</title>
</head>
<body bgcolor="white">

<p>下面展示了Jsp的四种组合XML XSLT的方式:
<p>下面使用apply方式,将已经存在的employees.xml和employeeList.xsl结合在一起

<xsl:apply xml="/xml/employees.xml" xsl="/xml/employeeList.xsl"/>
<hr>


<p>下面是使用已经存在employeeList.xsl 然后在Jsp中自己直接写入XML数据.


<xsl:apply xsl="/xml/employeeList.xsl">
<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
<employee id="123">
<first-name>John</first-name>
<last-name>Doe</last-name>
<telephone>800-555-1212</telephone>
</employee>
<employee id="456">
<first-name>Jane</first-name>
<last-name>Smith</last-name>
<telephone>888-555-1212</telephone>
</employee>
<employee id="789">
<first-name>George</first-name>
<last-name>Taylor</last-name>
<telephone>555-555-1212</telephone>
</employee>
</employees>
</xsl:apply>
<hr>

<p>下面使使用include调用的办法,这样一个XSLT样式可以适应不同的XML文件。

<xsl:apply xsl="/xml/employeeList.xsl">
<xsl:include page="/xml/employees.xml"/>
</xsl:apply>
<hr>

<p>下面是使用import方式,在page-scope(类似scope="page")中导入XML文件</p>

<xsl:import id="data" page="/xml/employees.xml"/>
<xsl:apply nameXml="data" xsl="/xml/employeeList.xsl"/>

</body>


  在上面程序中,展示了四种Jsp组合XML XSLT的方式,基本可以满意我们的需要。注重上面的XML文件路径是"/xml/",这是相对Tomcat容器的绝对路径。

  我们简朴看一下employeeList.xsl和employees.xml内容:

  employeeList.xsl类似html中的CSS,主要是对XML中数据显示方法进行定义:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employees">
<table border="1" width="100%">
<tr>
<th>ID</th>
<th>Employee Name</th>
<th>Phone Number</th>
</tr>
<xsl:for-each select="employee">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<xsl:value-of select="last-name"/>,
<xsl:value-of select="first-name"/>
</td>
<td>
<xsl:value-of select="telephone"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>



employees.xml

<?xml version="1.0" encoding="ISO-8859-1"?>


<employees>
 <employee id="123">
  <first-name>John</first-name>
  <last-name>Doe</last-name>
  <telephone>800-555-1212</telephone>
 </employee>

 <employee id="456">
  <first-name>Jane</first-name>
  <last-name>Smith</last-name>
  <telephone>888-555-1212</telephone>
 </employee>

  <employee id="789">
  <first-name>George</first-name>
  <last-name>Taylor</last-name>
  <telephone>555-555-1212</telephone>
 </employee>
</employees>


  假如我们在employees.xml顶部加入:


<?xml:stylesheet type="text/xsl" href="catalog.xsl"?>

  用支持XML的IE 5.0以上浏览器调用,其显示页面就和Apply.jsp显示页面是相同的。
返回类别: 教程
上一教程: 使用JAVA DATA OBJECT(JDO)存放持久性数据
下一教程: JAVA动画编程基础第三部分

您可以阅读与"JSP结合XML+XSLT将输出转变为HTML格式"相关的教程:
· JSP与XML的结合
· JSP与JAVAMAIL之4(发送HTML格式邮件)
· 将文本格式的文章转变为html/xml格式文本的功能封装到Javabean
· JAVA数据库编程中查询结果的表格式输出
· javascript向jsp传递参数的一种手段:使用xmlhttp对象
    微笑服务 优质保证 索取样品