使用jsp 加上tablib標識庫,我們可以完成這種轉換。
著名open source項目組jakarta.apache.org推出的系列標識庫中,就有這個功能的tanglib:http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html
按照jakarta配置方法,有點繁瑣,需要修改或定義web.xml,本人經過摸索,使用下列相當簡單的辦法,就可以使jsp能成功運行xsl這個標識庫了。
xsl標識庫有三個關鍵包:
1.將這三個包放置到tomcat的common/lib目錄下,或者直接放入classpath環境中。
2.在jsp中調用標識庫:
原來jakarta推薦方法是:
這就需要在/web-inf/web.xml下定義一下http://jakarta.apache.org/taglibs/xsl-1.0指向。如:
這種做法雖然很標準,但是,如果你的容器一直使用tomcat,就完全不必了。
我們的做法是:
我們以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>
新聞熱點
疑難解答