|
JSTL - Sample XSLT transformation |
|
|
JSTL provides a simple tag - <x:transform>, that should handle most of your XSLT-transformation needs.
The <x:transform> tag's two most basic attributes are xml and xslt.
Given a source XML document (xml) and an XSLT stylesheet (xslt), the tag's default behavior is to apply
the stylesheet to the source and output the result into the page.
This simple use of the <x:transform> tag accepts a source document and a stylesheet, both of which
are exposed by <c:import> tags. The tag then applies the XSLT stylesheet to the document and outputs the result:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
////////////// sets xml variable
<c:set var="xml">
<paragraph>
This document uses <bold>unusual</bold> markup,
which we want to replace with <bold>HTML</bold>.
</paragraph>
</c:set>
////////////// sets xsl variable
<c:set var="xsl">
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="paragraph">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="bold">
<b><xsl:value-of select="."/></b>
</xsl:template>
</xsl:stylesheet>
</c:set>
////////////// uses xml and xsl:
<x:transform xml="${xml}" xslt="${xsl}"/>
|
Related Tips
|
For those of you who were wondering, the JSTL library includes the core, SQL, formatting and XML taglibs. I found myself searching the net for the JSTL XML taglib, when it\'s all bundeld in the jstl.jar library which simply needs to be dumped into the /WEB-INF lib directory of your web-app.