Jsp联合XML+XSLT将输出转换为Html格式
摘要:Jsp联合XML+XSLT将输出转换为Html格式
Jsp联合XML+XSLT将输出转换为Html格式
咱们知道 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="http://jakarta.apache.org/taglibs/xsl-1.0" 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" %>
咱们以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显示页面是一样的。
著名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="http://jakarta.apache.org/taglibs/xsl-1.0" 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" %>
咱们以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显示页面是一样的。