JSP combines XML+XSLT to convert output to HTML format

Source: Internet
Author: User
Tags xsl xslt tomcat

We know that XML+XSLT can be directly exported to XML-enabled browsers, such as IE more than 5.0, but we also have to consider that there are many browsers do not directly support XML, in this case, we need to convert the server to HTML output to the browser, This interim transitional approach may have to be used for some time.

Using JSP plus the Tablib identity Library, we can do this transformation.

The well-known open source project group Jakarta.apache.org launched a series of identity libraries, there is this feature tanglib:http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

According to the Jakarta configuration method, a bit cumbersome, need to modify or define Web.xml, I after groping, using the following fairly simple method, you can make JSP successfully run XSL this identity library.

The XSL Identity library has three key packages:

Xerces.jar can be obtained in http://xml.apache.org/.

Xalan.jar can be obtained in http://xml.apache.org/.

Xsl.jar get from http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

1. Place the three packages in the Common/lib directory of Tomcat or directly into the CLASSPATH environment.

2. Call the identity library in the JSP:

The original Jakarta recommended method is:

<%@taglib uri="http://jakarta.apache.org/taglibs/xsl-1.0" prefix="xsl" %>

This requires a http://jakarta.apache.org/taglibs/xsl-1.0 point to be defined under/web-inf/web.xml. Such as:

<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/xsl-1.0</taglib-uri>
<taglib-location>/WEB-INF/xsl.tld</taglib-location>
</taglib>

This is a standard practice, but if you have a container that uses Tomcat all the time, you don't have to.

Our approach is to:

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

Let's take the apply.jsp of the Jakarta XSL Taglib as an example, just to understand the relationship between the JSP XML XSLT:

apply.jsp

<% @taglib uri= "Xsl.jar" prefix= "xsl"%>
<title> Employee List </title>
<body bgcolor= "White"
The <p> shows the following methods for JSP's four combinatorial XML XSLT:
<p> Use the Apply method below to combine existing employees.xml and employeelist.xsl
<xsl:apply xml= "/xml/employees.xml" xsl= "/xml/employeelist.xsl"
<p> The following is written directly to XML data in the JSP using an already existing employeelist.xsl.
<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>
<p> The following methods are invoked with include, such that an XSLT style can be adapted to different XML files.
<xsl:apply xsl= "/xml/employeelist.xsl"
<xsl:include page= "/xml/employees.xml"/>
</xsl:apply>
<p> The following is to import an XML file in Page-scope (similar to scope= "page") by using the Import method </p>
<xsl:import id= "Data" page= "/xml/employees.xml"/>
<xsl:apply namexml= "Data" xsl= "/xml/employeelist.xsl"/>
</body>

In the above program, we show four kinds of JSP combined XML XSLT method, basically can meet our needs. Note that the XML file path above is "/xml/", which is the absolute path relative to the Tomcat container.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.