您正在使用 IPV4 [18.220.11.34] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
悟空收录网       [公益]保护绿色环境,构建和谐社会      

【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价      
[公益] 地球是我家,绿化靠大家      
2024年 劳动节 到期
2024年 端午节 039
2025年 元 旦 244
2025年 春 节 272
 
您现在的位置:首页 >> JSP >> 内容
本类新增
本类热门
Jsp结合XML+XSLT将输出转换为Html格式
内容摘要: 我们知道XML+XSLT就可以直接输出到支持XML的浏览器上,如IE5.0以上,但是,我们还要考虑到有不少浏览器不直接支持XML,在这种情况下,我们需要在服务器上进行转换成html输出到浏览器,这种临时过渡办法恐怕要在一段时间内一直要使用.使用Jsp加上tablib标识库,我们可以完成这种转换。著名opensource项目组jakarta.apache.or......
我们知道XML+XSLT就可以直接输出到支持XML的浏览器上,如IE5.0以上,但是,我们还要考虑到有不少浏览器不直接支持XML,在这种情况下,我们需要在服务器上进行转换成html输出到浏览器,这种临时过渡办法恐怕要在一段时间内一直要使用.使用Jsp加上tablib标识库,我们可以完成这种转换。

著名opensource项目组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推荐方法是:

<%@tagliburi="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,就完全不必了。

我们的做法是:

<%@tagliburi="xsl.jar"prefix="xsl"%>

我们以Jakarta的XSLtaglib附带的Apply.jsp为例,正好了解一下JspXMLXSLT三者之间的关系:

Apply.jsp

<%@tagliburi="xsl.jar"prefix="xsl"%>

<html>

<head>

<title>EmployeeList</title>

</head>

<bodybgcolor="white">

<p>下面展示了Jsp的四种组合XMLXSLT的方法:

<p>下面使用apply方法,将已经存在的employees.xml和employeeList.xsl结合在一起

<xsl:applyxml="/xml/employees.xml"xsl="/xml/employeeList.xsl"/>

<hr>

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

<xsl:applyxsl="/xml/employeeList.xsl">

<?xmlversion="1.0"encoding="ISO-8859-1"?>

<employees>

<employeeid="123">

<first-name>John</first-name>

<last-name>Doe</last-name>

<telephone>800-555-1212</telephone>

</employee>

<employeeid="456">

<first-name>Jane</first-name>

<last-name>Smith</last-name>

<telephone>888-555-1212</telephone>

</employee>

<employeeid="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:applyxsl="/xml/employeeList.xsl">

<xsl:includepage="/xml/employees.xml"/>

</xsl:apply>

<hr>

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

<xsl:importid="data"page="/xml/employees.xml"/>

<xsl:applynameXml="data"xsl="/xml/employeeList.xsl"/>

</body>

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

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

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

<?xmlversion="1.0"?>

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:templatematch="employees">

<tableborder="1"width="100%">

<tr>

<th>ID</th>

<th>EmployeeName</th>

<th>PhoneNumber</th>

</tr>

<xsl:for-eachselect="employee">

<tr>

<td>

<xsl:value-ofselect="@id"/>

</td>

<td>

<xsl:value-ofselect="last-name"/>,

<xsl:value-ofselect="first-name"/>

</td>

<td>

<xsl:value-ofselect="telephone"/>

</td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

employees.xml

<?xmlversion="1.0"encoding="ISO-8859-1"?>

<employees>

<employeeid="123">

<first-name>John</first-name>

<last-name>Doe</last-name>

<telephone>800-555-1212</telephone>

</employee>

<employeeid="456">

<first-name>Jane</first-name>

<last-name>Smith</last-name>

<telephone>888-555-1212</telephone>

</employee>

<employeeid="789">

<first-name>George</first-name>

<last-name>Taylor</last-name>

<telephone>555-555-1212</telephone>

</employee>

</employees>

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

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

用支持XML的IE5.0以上浏览器调用,其显示页面就和Apply.jsp显示页面是一样的。

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:使用JSP开发WebMail系统

 

下一篇:jsp中实现上传图片即时显示效果功能

发布日期:2023/4/27
手机扫二维码直达本页
发布时间:13:09:43
点  击:10
录  入:壹家怡园
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,奇安信网站卫士提供加速防护
运行时间载入中.....