Compiled XSLT stylesheet support in Kawa

The Kawa framework includes some support for XSLT. XSL (Extensible StyleSheet Language) Tranformations. XSLT is a popular language for transforming (including selecting and re-organizing) XML documents. The Kawa implementation treats an XSLT stylesheet as a program in the XSLT programming language, in the same way that other Kawa-supported languages (Scheme, XQuery, Emacs Lisp, and Common Lisp) are handled. Thus an XSLT stylesheets gets translated to Java bytecodes before execution, either on-the-fly, or saved to (one or more) .class files.

Example

The current implementation is little more than a proof-of-concept. However, the following is an example of a working stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="users">
<table border="1">
<thead><tr><th>userid</th><th>name</th><th>rating</th></tr></thead>
<xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="user_tuple">
<tr><xsl:apply-templates/></tr>
</xsl:template>

<xsl:template match="userid">
<td><xsl:apply-templates/></td>
</xsl:template>

<xsl:template match="name">
<td><xsl:apply-templates/></td>
</xsl:template>

<xsl:template match="rating">
<td><xsl:apply-templates/></td>
</xsl:template>
</xsl:stylesheet>

Assuming this is a file named MakeTable.xsl, and you have installed Kawa as the command/script kawa, you can compile the stylesheet using this command:

kawa --xslt --main -C MakeTable.xsl
This creates the compiled file MakeTable.class. This is a Java application, with a main method.

Now suppose you have a data file users.xml:

<users>
  <user_tuple>
    <userid>U01</userid>
    <name>Tom Jones</name>
    <rating>B</rating>
  </user_tuple>
  <user_tuple>
    <userid>U02</userid>
    <name>Mary Doe</name>
    <rating>A</rating>
  </user_tuple>
</users>

If you run this:

java MakeTable users.xml
you will get the resulting HTML code:
useridnamerating
U01 Tom Jones B
U02 Mary Doe A

Status

The following XSLT elements are handled:

xsl:stylesheet
xsl:apply-template
Only the default no-attribute case is supported. The mode attribute is implemented but untested.
xsl:value-of
The select attribute is handled and evaluated, but this has not been tested well.
xsl:template
The match attribute can only be a simple element name. The name attribute is handled.
xsl:if
Is handled, but the expression in the test attribute is probably not evaluated in the correct context.

Namespace handling has not been tested.

Trying out Kawa-XSLT

You need to get and install Kawa, as described in the
Kawa manual.
Per Bothner
Last modified: Thu Jun 13 18:10:08 PDT 2002