SoFunction
Updated on 2025-04-07

Jsp combined with XML + XSLT to convert output to Html format

We know that XML+XSLT can be directly output to browsers that support XML, such as IE 5.0 or above. However, we also need to consider that many browsers do not directly support XML. In this case, we need to convert it to html on the server and output it to the browser. This temporary transition method may have to be used for a period of time. Using Jsp plus the tablib identity database, we can complete this conversion.

The series of logo libraries launched by the famous open source project team have this function tanglib:/taglibs/doc/xsl-doc/

According to the jakarta configuration method, it is a bit cumbersome and needs to be modified or defined. After exploring, I can use the following simple methods to enable Jsp to successfully run the XSL identity database.

There are three key packages for the xsl identity library:
Can be obtained in /
Can be obtained in /
Get it from /taglibs/doc/xsl-doc/

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

2. Call the identity database in JSP:

It turns out that Jakarta's recommended method is:


<%@taglib uri="/taglibs/xsl-1.0" prefix="xsl" %>

This requires the definition of /taglibs/xsl-1.0 pointer under /WEB-INF/. like:


<taglib>
<taglib-uri>/taglibs/xsl-1.0</taglib-uri>
<taglib-location>/WEB-INF/</taglib-location>
</taglib>

Although this approach is very standard, if your container has been using tomcat, it is not necessary at all.

Our approach is:


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

Let’s take the included XSL taglib of Jakarta as an example to understand the relationship between Jsp XML XSLT:


<%@taglib uri="" prefix="xsl" %>
<html>
<head>
<title>Employee List</title>
</head>
<body bgcolor="white">

<p>The following shows four methods of combining XML XSLT by Jsp:
<p>The following uses the apply method to combine the existing and existing methods

<xsl:apply xml="/xml/" xsl="/xml/"/>
<hr>


<p> The following is the use of the existing one and then write the XML data directly in Jsp.


<xsl:apply xsl="/xml/">
<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
<employee >
<first-name>John</first-name>
<last-name>Doe</last-name>
<telephone>800-555-1212</telephone>
</employee>
<employee >
<first-name>Jane</first-name>
<last-name>Smith</last-name>
<telephone>888-555-1212</telephone>
</employee>
<employee >
<first-name>George</first-name>
<last-name>Taylor</last-name>
<telephone>555-555-1212</telephone>
</employee>
</employees>
</xsl:apply>
<hr>

<p>The following uses include call method, so that an XSLT style can adapt to different XML files.

<xsl:apply xsl="/xml/">
<xsl:include page="/xml/"/>
</xsl:apply>
<hr>

<p>The following is to use the import method to import the XML file in page-scope (similar to scope="page") </p>

<xsl:import page="/xml/"/>
<xsl:apply nameXml="data" xsl="/xml/"/>

</body>
 

In the above program, four methods of Jsp combining XML XSLT are shown, which can basically meet our needs. Note that the XML file path above is "/xml/", which is the absolute path relative to the Tomcat container.

Let's take a brief look at the content:

Similar to CSS in html, it mainly defines the data display method in XML:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http:///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>

 

<?xml version="1.0" encoding="ISO-8859-1"?>


<employees>
 <employee >
<first-name>John</first-name>
<last-name>Doe</last-name>
<telephone>800-555-1212</telephone>
 </employee>

 <employee >
<first-name>Jane</first-name>
<last-name>Smith</last-name>
<telephone>888-555-1212</telephone>
 </employee>

  <employee >
<first-name>George</first-name>
<last-name>Taylor</last-name>
<telephone>555-555-1212</telephone>
 </employee>
</employees>
 

If we add at the top:


<?xml:stylesheet type="text/xsl" href=""?> 

If you call it with an IE 5.0 or above browser that supports XML, the display page is the same as the display page.