XT

Version 19990725

Copyright (c) 1998, 1999 James Clark

See the file copying.txt for copying permission.

XT is an implementation in Java of XSL Transformations. This version of XT implements the WD-xslt-19990709 version of XSLT. Stylesheets written for earlier versions of the XSLT WD must be converted before they can be used with this version of XT.

This should be considered a beta release.

Usage

To use XT, you need:

Put xt.jar in your CLASSPATH, together with whatever is needed for your XML parser, and sax.jar if that isn't included with your XML parser. Then use the command:

java -Dcom.jclark.xsl.sax.parser=your-sax-driver com.jclark.xsl.sax.Driver source stylesheet result

To find a SAX parser, XT first uses the value of the system property com.jclark.xsl.sax.parser; if this is not set it uses the value of the system property org.xml.sax.parser; if this is not set it uses the class com.jclark.xml.sax.CommentDriver (This subclasses the normal XP SAX driver to provide support for comments; it is present only in XP version 0.5 or later; if you have an earlier version of XP use -Dcom.jclark.xsl.sax.parser=com.jclark.xml.sax.Driver instead.)

Alternatively under Windows you can use XT packaged as a Win32 executable. This includes XP and SAX. To use this, you will need to have the Microsoft Java VM installed (this is included with IE). Run this with the command:

xt source stylesheet result

XT API

The public interface to XT is com.jclark.xsl.sax.XSLProcessor which is implemented by com.jclark.xsl.sax.XSLProcessorImpl.

Limitations

The following features of the XSLT WD are not yet implemented:

Error reporting is rather haphazard. XT does not attempt to validate stylesheets. When it detects an error, it either silently recovers from it, or gives a fatal error. Forwards-compatible processing is not yet implemented.

Not much effort has yet been devoted to optimizing performance. The implementation of some features is known to be slow (notably xsl:number).

Extension Functions

A call to a function ns:foo where ns is bound to a namespace of the form http://www.jclark.com/xt/java/className is treated as a call of the static method foo of the class with fully-qualified name className. (XT does not currently make use of the xsl:functions element.) Hyphens in method names are removed with the character following the hyphen being upper-cased. Overloading based on number of parameters is supported; overloading based on parameter types is not. A non-static method is treated like a static method with the this object as an additional first argument. A constructor is treated like a static method named new. Extension functions can return objects of arbitrary types which can then be passed as arguments to other extension functions or stored in variables.

For example, the following

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns:date="http://www.jclark.com/xt/java/java.util.Date"
  xmlns="http://www.w3.org/TR/REC-html40"
  result-ns="">

<xsl:template match="/">
  <html>
    <xsl:if test="function-available('date:to-string') and function-available('date:new')">
      <p><xsl:value-of select="date:to-string(date:new())"/></p>
    </xsl:if>
  </html>
</xsl:template>

</xsl:stylesheet>

will print out the current date.

Types are mapped between XSLT and Java as follows:

XSLT typeJava type
stringjava.lang.String
numberdouble
booleanboolean
node-setcom.jclark.xsl.om.NodeIterator
result tree fragmentcom.jclark.xsl.sax.ResultTreeFragment

On return from an extension function, an object of type com.jclark.xsl.om.Node is also allowed and will be treated node-set; also any numeric type is allowed an will be converted to a number.

The demo directory has a couple of examples.

Non-XML output

The result-ns attribute can be used to get non-XML output. A result namespace of http://www.w3.org/TR/REC-html40 will make XT produce output that conforms to the HTML 4.0 Recommendation. To use this the xsl:stylesheet element should look like this:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns="http://www.w3.org/TR/REC-html40"
  result-ns="">
</xsl:stylesheet>

The result namespace can also use a java: URI. This should identify a class that implements the org.xml.sax.DocumentHandler and com.jclark.xsl.sax.OutputStreamConverter interfaces.

The com.jclark.xsl.sax.NXMLOutputHandler is one such class, which is included in XT. This supports a result namespace which is described by the following DTD:

<!ELEMENT nxml (escape*, (control|data)*)>
<!ATTLIST nxml encoding NMTOKEN #IMPLIED>
<!ELEMENT escape (#PCDATA|char)*>
<!ATTLIST escape char CDATA #REQUIRED>
<!ELEMENT control (#PCDATA|char|data|control)*>
<!ELEMENT data (#PCDATA|data|control)*>
<!ELEMENT char EMPTY>
<!ATTLIST char number NMTOKEN #REQUIRED>

The nxml element is the root element; the encoding attribute is the name of an encoding supported by your Java VM that will be used to encode characters as bytes.

The data element contains data. Within a data element control characters get escaped. The escape element specifies how a particular control character gets escaped.

The control element contains control information. Within a control element, all characters are output directly without escaping.

The char element allows the output of a character that is not allowed by XML (such as control-L).

For example, the following stylesheet

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
  xmlns="java:com.jclark.xsl.sax.NXMLOutputHandler"
  result-ns="">
<xsl:template match="/">
<nxml>
<escape char="\">\\</escape>
<data>&amp;&lt;&gt;\</data>
<control>&amp;&lt;&gt;\</control>
</nxml>
</xsl:template>
</xsl:stylesheet>

will output

&<>\\&<>\

Reporting Bugs

Please report bugs to me. When reporting bugs please be sure to include both a complete stylesheet and complete source document that illustrate the bug.

James Clark