JSP combined with XML + XSLT to convert the output to HTML Format

Source: Internet
Author: User
Tags xsl xslt

We know that XML + XSLT can be directly output to browsers that support XML, such as IE 5.0 or later. However, we also need to consider that many Browsers Do not directly support xml. In this case, we need to convert to HTML output to the browser on the server. I am afraid this temporary transition method will be used for a while. using JSP with tablib to identify the library, we can complete this conversion.

The famous open source project team jakarta.apache.org launched a series of identification library, there is this function of tanglib: http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

According to the configuration method of Jakarta, it is a little complicated. You need to modify or define web. xml. After self-exploration, you can use the following simple methods to make JSP successfully run the XSL identity library.

The XSL identification library has three key packages:
Xerces. jar can be obtained at http://xml.apache.org/
Xalan. jar can be obtained at http://xml.apache.org/
XSL. jar is obtained from http://jakarta.apache.org/taglibs/doc/developer-doc/intro.html

1. Place the three packages in the common/lib directory of Tomcat, or directly put them in the classpath environment.

2. Call the identification library in JSP:

The original recommended methods for Jakarta are:

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

This requires defining http://jakarta.apache.org/taglibs/xsl-1.0in/WEB-INF/Web. xml. For example:

<Taglib>
<Taglib-Uri> http://jakarta.apache.org/taglibs/xsl-1.0 </taglib-Uri>
<Taglib-location>/WEB-INF/XSL. TLD </taglib-location>
</Taglib>

Although this method is very standard, if your container has been using tomcat, it is completely unnecessary.

Our practice is:

<% @ Taglib uri = "XSL. Jar" prefix = "XSL" %>

Take apply. jsp with XSL taglib of Jakarta as an example to understand the relationship between jsp xml xslt:

Apply. jsp

<% @ Taglib uri = "XSL. Jar" prefix = "XSL" %>
<HTML>
<Head>
<Title> employee list </title>
</Head>
<Body bgcolor = "white">

<P> The following shows four methods for combining xml xslt in JSP:
<P> the following uses the apply method to combine existing employees. XML with employeelist. XSL.

<XSL: Apply xml = "/XML/employees. xml" XSL = "/XML/employeelist. XSL"/>
<HR>

<P> The following describes how to use employeelist. XSL to directly write XML data in JSP.

<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> The following describes how to call include. Such An XSLT style can adapt to different XML files.

<XSL: Apply XSL = "/XML/employeelist. XSL">
<XSL: Include page = "/XML/employees. xml"/>
</XSL: Apply>
<HR>

<P> The following describes how to import an XML file in page-scope (similar to scope = "page") using the import method. </P>

<XSL: Import id = "data" page = "/XML/employees. xml"/>
<XSL: Apply namexml = "data" XSL = "/XML/employeelist. XSL"/>

</Body>

AboveProgramShows four JSP combination xml xslt methods, which can basically meet our needs. Note that the above XML file path is "/XML/", which is the absolute path relative to the Tomcat container.

Let's take a brief look at the content of employeelist. XSL and employees. xml:

Employeelist. XSL is similar to CSS in HTML. It mainly defines the data display mode in 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>

If we add:

<? XML: stylesheet type = "text/XSL" href = "catalog. XSL"?>

The display page is the same as the display page of apply. jsp.

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.