Turbine 2.x User's Guide: Using the XSLT Service

The following is a basic overview of using the XSLT service in Turbine 2.2 to parse and display XSL templates.

First, a little overview of XSLT. XSL stylesheets are templates used to generate a certain type of SGML documents (HTML, XML, DocBook, etc) from an XML file. Obviously, its most popular use, especially in relation to Turbine, is to generate HTML based on an XML document.

There is not much in the way of configuration to actually use the service. You simply need to update one parameter in TurbineResources.properties:

services.XSLTService.path = /path/to/your_stylesheets

So, here is a basic XSL transformation done with the service. This code would be put within a screen class, so the Context contains the HTML-formatted String generated by the XSLTService.

  // Get your XML.  I'm using a String (as the XML is stored in the DB) 
  // but you could use a FileReader instead 
  String xmlSchema = getXMLSchema(); 

  // This is just a filename.  The location for the 
  // templates is defined in TurbineResources.properties 
  String xslTemplate = "MyTemplate.xsl"; 

  // Set up a StringReader for the XML String: 
  Reader reader = new StringReader(xmlSchema); 

  // Grab an instance of the service: 
  ["TurbineXSLTService"] xslt = new ["TurbineXSLTService"](); 

  // Transform the XML against your XSL template: 
  String sOutput = xslt.transform(xslTemplate, reader); 

  // Put the HTML formatted String in the Context for display: 
  context.put("formOutput", sOutput); 

If you're using Velocity, you can now display the newly transformed HTML within your template:


and you're finished.

For more information about the XSLT language:

  • No labels