新聞中心
我們知道JSP XML XSLT就可以直接輸出到支持XML的瀏覽器上,如IE 5.0以上,但是,我們還要考慮到有不少瀏覽器不直接支持XML,在這種情況下,我們需要在服務(wù)器上進行轉(zhuǎn)換成html輸出到瀏覽器,這種臨時過渡辦法恐怕要在一段時間內(nèi)一直要使用.

成都創(chuàng)新互聯(lián)是專業(yè)的阿克陶網(wǎng)站建設(shè)公司,阿克陶接單;提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行阿克陶網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
使用JSP 加上tablib標識庫,我們可以完成這種轉(zhuǎn)換。
著名open source項目組jakarta.apache.org推出的系列標識庫中,就有這個功能的tanglib:http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html
按照jakarta配置方法,有點繁瑣,需要修改或定義Web.xml,本人經(jīng)過摸索,使用下列相當簡單的辦法,就可以使JSP能成功運行XSL這個標識庫了。
xsl標識庫有三個關(guān)鍵包:
◆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環(huán)境中。
2.在JSP中調(diào)用標識庫:
原來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三者之間的關(guān)系:
Apply.JSP
- <%@taglib uri="xsl.jar" prefix="xsl" %>
- <html>
- <head>
- <title>Employee List</title>
- </head>
- <body bgcolor="white">
下面展示了Jsp XML XSLT的方法:
下面使用apply方法,將已經(jīng)存在的employees.xml和employeeList.xsl結(jié)合在一起
- <xsl:apply xml="/xml/employees.xml" xsl="/xml/employeeList.xsl"/>
- <hr>
下面是使用已經(jīng)存在employeeList.xsl 然后在Jsp中自己直接寫入XML數(shù)據(jù).
- <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>
在上面程序中,展示了四種JSP XML XSLT的方法,基本可以滿足我們的需要。注意上面的XML文件路徑是"/xml/",這是相對Tomcat容器的絕對路徑。
我們簡單看一下employeeList.xsl和employees.xml內(nèi)容:
employeeList.xsl類似html中的CSS,主要是對XML中數(shù)據(jù)顯示方式進行定義:
- <?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>
【編輯推薦】
- 入侵JSP網(wǎng)站服務(wù)器
- 基于JSP技術(shù)的網(wǎng)絡(luò)
- JSP變量在規(guī)范中稱作范圍變量
- ASP優(yōu)點和JSP優(yōu)點總結(jié)
- 自定義JSP標簽由淺到深詳細講解
分享標題:JSP XML XSLT將輸出轉(zhuǎn)換HTML
本文URL:http://www.dlmjj.cn/article/djicpgs.html


咨詢
建站咨詢
