Saturday, November 27, 2010

Extending XSLT with Java Static Methods

Now, this time my blog post is about ; using Java to extend XSLT functionality to get more out of XSLT. I'm going to start-off with simple way of extending XSLT by using Static Java Methods. These methods can be invoked from your XSLT Templates to get your complex calculations and transformations done in Java that cannot be done with XSLT or which is more difficult to read/maintain in XSLT.

Ok, we will get into the sample now. I have a sample XML file that has Student Records (First Name, Second Name, Department Id and Grade) - My simple objective is to generate a HTML Page with this records that shows the table of Student names, their department name and grade. Huh!... My XML is having only the Department Id and I need the department name in my HTML. Well, I can write a xslt choose block to select the department name when I'm given department id, but whenever I have changes in this mapping I have to modify my XSLT and also this is not going to be the primary data source as the university will have its own database that has Department details. So, I'm going to query the DB and use the name in my XSLT. Well, though I can do that; I don't want to complicate my example - I have a flat Java properties file that will have the DepartmentId=DepartmentName mapping and I'll use it in my XSLT invoked Java methods. The same way, grade name is also fetched from a properties file.

<?xml version="1.0" encoding="UTF-8"?>

The output HTML page we will get is the following

<html xmlns:jClass="xalan://ExtendedXSLT" xmlns:xalan="" xmlns:fn="" xmlns:xs="">
        <title>Students List</title>
        <h1>Students List</h1>
        <table border="1">
                    <th>Department Id</th>
                    <th>Department Name</th>
                    <th>Grade Id</th>
                    <th>Grade Name</th>
                    <td>First Name</td>
                    <td>Second Name</td>
                    <td>Third Name</td>
                    <td>Fourth Name</td>
                    <td>Fifth Name</td>

You can download all the files that are discussed here along with the Java Source Code and XSLT from the following URL -

The Java Class ExtendedXSLT does nothing more than loading the Department and Grade Properties, setting the System Property to use Xalan Transformer and transforming the XML using our XSLT. But, it has two public static methods that are not used anywhere in the main method. But, these methods are used in XSLT to get the mapping from DepartmentId and Grade Id. The magic is done by adding the xml namespace declaration in XSLT that xalan is the prefix for xalan's Namespace ( ) and jClass is the prefix that will refer to our ExtendedXSLT Class. This is done by using the function in xalan namespace. You can see that our XSLT will have a prefix defined called jClass that is mapped to "xalan://ExtendedXSLT". This statement helps us to invoke the methods in ExtendedXSLT class by using the jClass prefix. You can also refer to more than one class by specifying them in the same prefix separated by comma. Xalan will take care of locating the method.

Though this method is simple to extend the XSLT, this can be very useful when you want to do complex calculations, type conversions, external entity calls, etc. Hope, this will be useful for few people out there.

No comments:

Post a Comment