20
Mar
10

Java Command Line XSLT Transformation


This page describes the process of transforming XML using Java via the command line.

Not too long ago Java integrated the XSLTC compiler that allowed developers to compile their stylesheets into class files. These class files can be run to perform transformations on xml input.

Transformation using Command Line

As of JDK 1.5 you can run the following commands to do a translation.

The first step is to compile the style sheet into a .class file.

java com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile [-o <output>]
      [-d <directory>] [-j <jarfile>] [-p <package>]
      [-n] [-x] [-u] [-v] [-h] { <stylesheet> | -i }

Next we use the compiled transformation to convert a document over.

java com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform
     [-j <jarfile>] [-x] {-u <document_url> | <document>} <class>
     [<name1>=<value1> 

XML document

In the following example we define an xml source.

src/main/resources/com/test/test.xml

<?xml version="1.0"?>
<hello-world>
   <greeter>An XSLT Programmer</greeter>
   <greeting>Hello, World!</greeting>
</hello-world>

Stylesheet

src/main/resources/com/test/test.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/hello-world">
    <HTML>
      <HEAD>
        <TITLE></TITLE>
      </HEAD>
      <BODY>
        <H1>
          <xsl:value-of select="greeting"/>
        </H1>
        <xsl:apply-templates select="greeter"/>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="greeter">
    <DIV>from <I><xsl:value-of select="."/></I></DIV>
  </xsl:template>
</xsl:stylesheet>

Compile and Transform

java com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile test.xsl

At this point you should have test.class in your current directory.

java com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform test.xml test

At this point you should see some results being printed out to the screen.

Advertisements

3 Responses to “Java Command Line XSLT Transformation”


  1. 1 Csaba Veres
    April 8, 2010 at 2:34 am

    Thanks, very useful!
    I assume the -x option can be used to set the Java heap space?

  2. 3 brant
    March 20, 2012 at 2:39 pm

    Wow this is cool. However I just found out it doesn’t support XSLT 2.0 It’ll compile a 2.0 into a class file fine but you’ll get errors like: Translet errors:Unsupported XSL element ‘http://www.w3.org/1999/XSL/Transform:analyze-string’ (I’m trying to use Regexs).


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 74 other followers

March 2010
S M T W T F S
« Feb   Apr »
 123456
78910111213
14151617181920
21222324252627
28293031  

Blog Stats

  • 801,304 hits

%d bloggers like this: