Extension functions in XT (was Re: HTML forms with XSLT slideshow question)

Subject: Extension functions in XT (was Re: HTML forms with XSLT slideshow question)
From: Edd Dumbill <edd@xxxxxxxxxxxxx>
Date: Mon, 27 Sep 1999 12:30:56 +0000
On Mon, Sep 27, 1999 at 12:49:28PM +0700, James Clark wrote:
> Beyond that is the issue of allowing extension functions to be written
> in other languages.  In particular it would be nice to support
> JavaScript (using Rhino).

I've had some success using FESI with XT
<http://home.worldcom.ch/~jmlugrin/fesi/>, license here:
<http://home.worldcom.ch/~jmlugrin/fesi/copyright.html>

using a small glue class to define the following ECMAScript extension
functions:

ecma:include-file(), interprets an external ECMAScript file
ecma:interpret(), interprets the argument as ECMAScript and uses the
result as a resultfragment
ecma:interpret-silent(), interprets the argument as ECMAScript and
suppresses any return value

I've not tried yet with JPython, but I don't imagine it would be too
difficult (I can send the source code to anyone who's interested).

I can then invoke the ECMAScript either explicitly in my stylesheet or
define a template to catch a specific tag and then execute its text
contents as script.

Some obvious unwieldiness occurs, e.g. defining a function to wrap
double quotes around a string:

<xsl:value-of select="ecma:interpret('
 function quote(d) { return &quot;\&quot;&quot;+d+&quot;\&quot;&quot;; }
')"/>

Although this goes away if all the functions are put in an external
file and I use include-file() instead.

One problem that I've come across, and I may have already received an
answer from James (but I had some ISP problems recently and so wouldn't
know if I had or not!), is this:

It would be very very useful if extension functions could return Nodes
to be subsequently fed to the stylesheet.  However I have a suspicion
that extension functions are only allowed within the WD to return parts
of a Result.  What I'd like to be able to do is define a function which
returns a Node structure, and then say:

<xsl:apply-templates select="myfn:createMeNodes()"/>

An example application of this would be in servlet pages for grabbing in
session information from the web server and formatting it.

I know it's possible to hand back Nodes in XT and have it work properly,
as I can construct an identity function which just hands back the Nodes
it gets, but that's as far as I can get.  I suspect this is because of
what James writes here:

> Closely related is the issue of supporting extension elements.  I
> haven't yet tried to implement this, so I'm very open to suggestions. 
> The one major constraint that may be peculiar to XT is that the public
> interfaces to XT are based on SAX not DOM, and I want to keep it that
> way.

Does this mean there's a SAX way I can do what I want?

Or is what I'm trying to do more suited to extension elements?

> James

Help appreciated.

Edd


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread