Tutorials

Task-driven tutorials and recipes for PageSeeder

How to run XSLT from Eclipse

Skills requiredXML
Time required (minutes)15
Intended audienceDeveloper
DifficultyEasy
CategoryDocument

Objective

XSLT is a W3C standard programming language primarily designed to process XML content. One task it is particularly well suited to is transforming XML to another syntax such as plain text or HTML or to an alternate XML structure. This tutorial shows how to do an XSLT transformation using the Eclipse IDE.

Prerequisites

To complete this tutorial requires:

  • A computer with the Eclipse IDE installed (the Eclipse IDE for Java EE developers is recommended as it includes packages for XML and XSLT editing). It can be download from http://www.eclipse.org/downloads/ .
  • The Buildship plugin installed on Eclipse (it can be downloaded in Eclipse via the Eclipse Marketplace).

Tutorial

Installing an XSLT processor

To interpret and run XSLT code requires an XSLT processor. The steps below will install the Saxon XSLT processor in Eclipse.

  1. Create a new general project in Eclipse (e.g. xslt-transform).
  2. Create a new file in the root of the project called build.gradle and copy the code below into it.
  3. Right-click on the project and select Configure > Add Gradle Nature.
buildscript {
  ext.data_folder = 'sample'
  ext.xslt_file = 'sample.xsl'
  ext.input_file = '/'
  ext.output_file = '/'
  ext.xslt_param1 = ''
  ext.xslt_param2 = ''
  ext.xslt_param3 = ''
  ext.saxon_version = '9.8.0-14'

  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "net.sf.saxon:Saxon-HE:$saxon_version"
  }
}

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
  mavenCentral()
}

configurations {
  xslt
}

dependencies {
  xslt "net.sf.saxon:Saxon-HE:$saxon_version"
}

// Ensure we have an output dir
project.file("output/$data_folder").mkdirs()

// Transform using XSLT
task transform(group: 'xslt', type: JavaExec) {
  classpath = configurations.xslt
  main = 'net.sf.saxon.Transform'
  inputs.dir  'input/$data_folder'
  inputs.file  'src/main/xslt/$xslt_file'
  args(
      "-s:input/$data_folder$input_file",
      "-xsl:src/main/xslt/$xslt_file",
      "-o:output/$data_folder$output_file",
      "$xslt_param1",
      "$xslt_param2",
      "$xslt_param3"
  )
}

// Rename output files
task rename(group: 'xslt', type: Copy) {
    from "output/$data_folder"
    into "rename/$data_folder"

    rename '(.*)\\.xml', '$1.psml'
}

Run the XSLT code

  1. Create an input/[data folder] folder in the project (e.g. input/films) and copy the input XML files (*.xml) to it.
  2. Create a src/main/xslt folder in the project and copy the XSLT code file (e.g. films.xsl) to it.
  3. Edit the build.gradle file and modify the ext.data_folder  and ext.xslt_file to match the above. For example:
    ext.data_folder = 'films'
    ext.xslt_file = 'films.xsl'
    
  4. Click on the Gradle Tasks tab in the right hand Eclipse panel, expand the project above and click on xslt > transform. This will transform all XML files in the input/[data folder] and put the result files in the output/[data folder].
  5. Click on the project in the left hand Eclipse panel and press the F5 key to refresh the folders. Then open the files under output/[data folder] to check the content.

Note

  • To only transform a single file in the input folder edit ext.input_file  and ext.output_file with the desired filenames. For example:
    ext.input_file = '/myinput.xml'
    ext.output_file = '/myoutput.xml'
    
  • To set XSLT parameters for the transform edit ext.xslt_param[N] with the desired values. For example:
    ext.xslt_param1 = 'output-base=.'
    ext.xslt_param2 = 'filename=word export'
    ext.xslt_param3 = 'split=true'
    
    If more parameters are needed they can be added under the task transform() code. For a full list of valid args() see http://www.saxonica.com/documentation/#!using-xsl/commandline  .
  • When using the document() function in XSLT the path will be relative to the src/main/xslt folder, so the referenced XML files need to be in this folder or use '..' in the path to point to the input folder e.g. document('../../../input/sample/sample.xml')

Rename the output files to .psml (optional)

If the output files have a .xml extension but are in PSML format to by uploaded to PageSeeder then rename them to .psml as follows:

  1. Click on the Gradle Tasks tab in the right hand Eclipse panel, expand the project above and click on xslt > rename.
  2. Click on the project in the left hand Eclipse panel and press the F5 key to refresh the folders. Then open the files under rename/[data folder] to check the content.

 

 

Created on , last edited on