It's very simple, and uses the Java 1.4 (if I recall correctly) transformation classes, thus reducing the need for any external dependencies. I hope it's some use!

The plugin takes two parameters:

  • xml - An XML file which will be used as the source of the transformation.
  • xsl - An XSL file which will be used to perform the transformation.

ChrisLisle

Here's the source:


package net.panonet;

import java.util.Map;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.ecyrd.jspwiki.WikiContext;

import com.ecyrd.jspwiki.plugin.WikiPlugin;
import com.ecyrd.jspwiki.plugin.PluginException;

import java.io.StringWriter;

/**
 * JSPWiki plugin for processing XML files into HTML in a Wiki page using XSLT.
 *
 * The plugin has two parameters from the perspective of the Wiki page:
 * 
 * xsl='<xslfile>'    Name of the XSLT file to use
 * xml='<xslfile>'    Name of the XML file to process
 */
public class XSLTPlugin implements WikiPlugin
{
    /**
     * Main plugin entry point.
     */
    public String execute(WikiContext context,
                          Map params)
        throws PluginException
    {
        // Get the names of the XML and XSL files to use
        String xsl = (String)params.get( "xsl" );
        String xml = (String)params.get( "xml" );
        
        String result = "";
        
        try
        {
            // Create a Transformer
            TransformerFactory transformerFactory
                = TransformerFactory.newInstance();
            Transformer transformer
                = transformerFactory.newTransformer( new StreamSource( xsl ) );

            // Transform the given XML file using the given XSL file, placing
            // it in a String.
            StringWriter resultWriter = new StringWriter();
        
            transformer.transform( new StreamSource( xml ),
                                   new StreamResult( resultWriter ) );
            
            result = resultWriter.toString();
        }
        catch( Exception e )
        {
            System.out.println( e.toString() );
            throw new PluginException( "Exception performing transformation.",
                                       e );
        }
        
        return result;
    }
}


FrancoisParlant (2003-oct-25)

Thanks for this plugin.

It works great


Karsten_Gresch (2004-Mar-01) Thanks for your contribution. Unfortunately I receive the following error when trying to use it:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\WINNT\system32\daily_words.xsl

Here, daily_words.xsl is a stylesheet that transforms a XML file with a motto for each day, i.e. "ephemerides".

What do I need to do that the plugin isn't looking for the system32 path?` Thanks!


DenisBalazuc (2003-Dec-17)

For your convenience, I have built the above code into the xslt-plugin.jar file (see attachments on this page). Libraries used for building/running:

  • j2ee.jar
  • xerces.jar
  • xalan.jar
Depending on your Web Engine, you might or might not have to add those to your JSPWiki WebApp.
Jawe (2004-May-29)

I've created a slightly modified version of your plugin, hope you don't mind :) It resolves relative paths in the xml and xsl parameters to the wiki's configured base URL. I was just feeling uncomfortable allowing to include any (xml) file with that plugin.

This should also get rid of issues with servlet engines resolving relative paths to their program dir, or even worse to the windows\system32 dir, like IIS does.

--> Source Code | --> Demo


Mock (2005-Jan-12)

I've modified Jawe's version a little bit so that you can specify Wiki Attachments and Pages as the xml and xsl parameters. The Source code is attached

--> Source Code(info)


(2005-Jun-22) Does anyone know how to make this plugin use Saxon instead of Xalan? I have tried putting Saxon.jar in the wiki web-inf/lib but it still uses Xalan for some reason. Any thoughts?


Not really sure what to do with this plugin ? can someone please give me a hand in getting this set-up i.e. set-up set-up where to install the plugins and who to refer to the plugin via a wiki page.

--newbie, 17-Aug-2006


--AnonymousCoward, 07-Oct-2006

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
java
XSLTPlugin-WikiPagesAndAttachm... 4.1 kB 1 12-Jan-2005 21:31 192.18.43.5
jar
xslt-plugin.jar 1.5 kB 1 17-Dec-2003 23:09 204.19.176.27
« This page (revision-18) was last changed on 07-Oct-2006 13:45 by AnonymousCoward