Skip to main content

 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

A programming language with the status of a W3C Recommendation , XSLT is primarily designed to process XML content. It is particularly well suited to transforming XML from one syntax to another. This could be to a different XML structure, such as XHTML or HTML. Or to a non-XML syntax, such as plain text or JSON. This tutorial shows how to use the Eclipse IDE to process an XSLT transformation.

Prerequisites

To complete this tutorial requires:

  • A computer with the Eclipse IDE installed – to simplify the setup, use the Eclipse IDE for Java EE developers, which includes packages for XML and XSLT editing. Download it from  http://www.eclipse.org/downloads/ .
  • The Buildship plugin installed on Eclipse – download this in Eclipse, via the Eclipse Marketplace.
  • Only Gradle v4.9 or earlier is supported in this tutorial – configure this by editing the gradle/wrapper/gradle-wrapper.properties and changing the version to 4.9.
  • The sample files for this tutorial are available on GitHub 

Tutorial

Installing an XSLT processor

Running XSLT code requires an XSLT processor. The following steps will install the Saxon XSLT processor in Eclipse.

  1. Create a new general project in Eclipse (e.g. xslt-transform).
  2. In the project root, create a new file 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 a project data folder (e.g. input/films) and copy the input XML files (*.xml) to it.
  2. Create a project code folder (e.g.src/main/xslt) and copy the XSLT code file (films.xsl) to it.
  3. Modify the build.gradlefile so the ext.data_folder andext.xslt_file match the above, for example:
    ext.data_folder = 'films'
    ext.xslt_file = 'films.xsl'
    
  4. In the right hand Eclipse panel, click on the Gradle Tasks tab to expand the project, then click on xslt > transform to process the XML files from theinput/[data folder]to theoutput/[data folder].
  5. In the left hand Eclipse panel, select the project and refresh the folders by pressing F5. Open the files under output/[data folder] to check the content.
  • To transform a single file from 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 parameters on the XSLT 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'
    
     Add additional parameters to 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.  To reference XML files, put them in this folder, or point to the input folder by using '..' in the path e.g. document('../../../input/sample/sample.xml')

Rename the output files to .psml (optional)

If the output files have a .xml extension but use the PSML format for upload to PageSeeder, use the following to change the extension to .psml:

  1. In the right hand Eclipse panel, select the Gradle Tasks tab, expand the project and click on xslt > rename.
  2. In the left hand Eclipse panel, select the project and refresh the folders by pressing F5. Open the files under rename/[data folder] to check the content.
Created on , last edited on

Available tutorials