JSP combines XML+XSLT to convert output to HTML

Source: Internet
Author: User
Tags xsl xslt tomcat
We know that XML + XSLT can be directly output to a browser that supports XML, such as IE 5.0 or above, but we also have to consider that many browsers do not directly support XML. In this case, we need to Converting to html output to the browser, I am afraid that this temporary transition method will be used for a period of time.

Using Jsp and tablib identification library, we can complete this conversion.

功能 Tanglib has this function in the series of identity libraries launched by the famous open source project group jakarta.apache.org: http://jakarta.apache.org/taglibs/doc/xsl-doc/intro.html

According to the jakarta configuration method, it is a bit tedious. You need to modify or define Web.xml. After exploring, I can use the following quite simple methods to make Jsp successfully run the XSL identity library.

Xsl identity library has three key packages:

Xerces.jar is available at http://xml.apache.org/

Xalan.jar is available at http://xml.apache.org/

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

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

2. Call the identity library in JSP:

The original Jakarta recommended method is:

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

This requires defining http://jakarta.apache.org/taglibs/xsl-1.0 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>
</ Tagtag>

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

Our approach is:

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

Let's take the Apply.jsp included with Jakarta's XSL taglib as an example to understand the relationship between the three Jsp XML XSLT:

Apply.jsp

<% @ taglib uri = "xsl.jar" prefix = "xsl"% >
<Html>
<Head>
<Title> Employee List </ title>
</ Head>
< body bgcolor = "white" >
<P> Jsp's four methods of combining XML XSLT are shown below:
<P> The apply method is used below to combine the existing employees.xml and employeeList.xsl
< xsl: apply xml = "/ xml / employees.xml" xsl = "/ xml / employeeList.xsl" / >
<Hr>

<P> The following is to use the existing employeeList.xsl and then write the XML data directly 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 uses the include call method, so that 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 is the use of the import method to import XML files in page-scope (similar to scope = "page").
< xsl: import id = "data" page = "/ xml / employees.xml" />
< xsl: apply nameXml = "data" xsl = "/ xml / employeeList.xsl" / >
</ Body>
In the above program, four Jsp combined XML XSLT methods are shown, which can basically meet our needs. Note that the above XML file path is "/ xml /", which is an absolute path relative to the Tomcat container.

Let's take a brief look at the contents of employeeList.xsl and employees.xml:

EmployeeList.xsl is similar to CSS in html, which 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

<? Xmlversion = "1.0" encoding = "ISO-8859-1"? >
< employees >
< employeeid = "123" >
<First-name> John </ first-name>
<Last-name> Doe </ last-name>
< telephone > 800-555-1212 < / telephone >
< / employee >
< employeeid = "456" >
<First-name> Jane </ first-name>
<Last-name> Smith </ last-name>
< telephone > 888-555-1212 < / telephone >
< / employee >
< employeeid = "789" >
<First-name> George </ first-name>
<Last-name> Taylor </ last-name>
< telephone > 555-555-1212 < / telephone >
< / employee >
</ Employees>

If we add at the top of employees.xml:

<? Xml: stylesheet type = "text / xsl" href = "catalog.xsl"? >

调用 Called with a browser that supports XML IE 5.0 and above, the display page is the same as the Apply.jsp display page.
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.