Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336518
  • 博文数量: 1051
  • 博客积分: 53280
  • 博客等级: 大将
  • 技术积分: 6670
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:21
文章分类

全部博文(1051)

文章存档

2011年(1)

2008年(1050)

我的朋友

分类:

2008-09-09 16:47:34

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

  使用Jsp 加上tablib标识库,我们可以完成这种转换。

  著名open source项目组jakarta.apache.org推出的系列标识库中,就有这个功能的tanglib:

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

  xsl标识库有三个关键包:

  ·xerces.jar 可以在中得到

  ·xalan.jar 可以在中得到

  ·xsl.jar 从得到

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

  2.在JSP中调用标识库:

  原来Jakarta推荐方法是:

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

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

  这就需要在/WEB-INF/web.xml下定义一下指向。如:

<taglib>
<taglib-uri></taglib-uri>
<taglib-location>/WEB-INF/xsl.tld</taglib-location>
</taglib>

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

  我们的做法是:

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

<%@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>

 

[1]  

【责编:Peng】

--------------------next---------------------

阅读(92) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~