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.
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.xslThis 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.xmlyou will get the resulting HTML code:
userid | name | rating |
---|---|---|
U01 | Tom Jones | B |
U02 | Mary Doe | A |
The following XSLT elements are handled:
xsl:stylesheet
xsl:apply-template
mode
attribute is implemented but untested.
xsl:value-of
select
attribute is handled and evaluated,
but this has not been tested well.
xsl:template
match
attribute can only be a simple element name.
The name
attribute is handled.
xsl:if
test
attribute is
probably not evaluated in the correct context.
Namespace handling has not been tested.