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> |
In the above program, we show four methods for Jsp combination of xml xslt, 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.