JSTL vs XSLTPosted: March 21, 2005
This post will be pretty technical so some of you might want to skip it…
I was playing around with JSTL (Java Standard Tag Library), particularly the XML library, and I found it to be much simpler to use than XSLT, which is the standard XML transform system (usually used to convert XML into HTML, but can be used to transform XML into pretty much anything).
The reason is that JSTL has the power of Java behind it, while XSLT has to do everything itself. XSLT works like this: you load the XML to be transformed, you load the XSL file (which contains transform instructions), and run them through a XSLT processor (usually around four lines of code). Since you're handing control over from your application to the processor, you have to make sure your XSL is doing everything it needs to once it's called. For example, if you want to do some sort of calculation (like the square root of a number) while you're transforming your XML, it's going to be really hard, since XSLT isn't built for that. You either have to put the calculated square root in the XML beforehand, or figure out a way to hack XSLT into doing it for you.
JSTL on the other hand lets you mix in whatever Java you want (via other tag libraries) while parsing the XML. In the previous example, you access the XML data using XML tags, and use Java's Expression Language (EL) to do the calculation. Then simply output the result.
What it boils down to is that JSTL doesn't need as much functionality in the XML library since it can make it up using other tag libraries. And if there are none that fit your needs, you can always write your own.
The only major area where XSLT completely dominates JSTL is recursion. In fact, I don't think JSTL even has recursion. If it does, it's probably an ugly hack, as opposed to XSLT, which is built for it. Of course, in JSTL, it's so easy to call XSLT that it's a non-issue anyway. You can just use XSLT wherever you need recursion done, and JSTL elsewhere.
The other major advantage XSLT has is that it's cross-everything, like XML. You can move your XSL stylesheets from Java to PHP to .Net etc etc, without having to change a thing. But, when it's so much easier to work with JSTL, why would you want to?