Re: [xml] shell function for XSLT

There are extensions available that provide an interface
between libxslt and a scripting language, such as TclXSLT
that allows the Tcl scripting language to be used to implement
arbitrary extension functions.  Similarly you may use Python or
Perl (and probably others).

While it is possible to create a general-purpose extension function
that allows you to execute any script code, I believe it would be a mistake
to follow this approach.  It is better to design your extension so that
the implementation is hidden from the stylesheet.  This makes the
stylesheet a little more portable since you may implement the
extension in another language.

For example, say I defined an extension function that evaluates a Tcl script
passed as a parameter:

<xsl:value-of select='tcl:eval("clock format [clock seconds] -format %m:%s")'/>

Now my XSL stylesheet is dependent on TclXSLT, since it contains actual
Tcl code (substitute Python, Perl, or whatever scripting language you choose
for Tcl in this case).

Instead, you should design your extension function to be language-neutral:

<xsl:variable name='now' select='time:now()'/>
<xsl:value-of select='time:format($now, "%m:%s")'/>

The "clock:now" and "clock:format" may be implemented in any language -
even in C.

Of course, for date-time functions specifically it would be even better to implement
the EXSLT functions.

Steve Ball

On 23/06/2004, at 9:56 PM, Nic Ferrier wrote:

What do people think about the idea of adding a shell function to
libxml2's xpath? I'm thinking of something that would make this XSLT

<xsl:template match="date">
  <xsl:variable name="date-str">
      <xsl:value-of select="."/>
    <xsl:value-of select="shell('date --date=$date-str --rfc-2822')"/>

when invoked with the following:

 <date>12 January 2004</date>

this would output:

 <converted-date>Mon, 12 Jan 2004 00:00:00 +0000</converted-date>

There would have to be restrictions on the shell command used.

$ would have to indicate XSLT parameters, not shell params.

quoting would have to be automatic (around xslt paramters for

entitys would have to be resolved automatically.

I'd think about investigating implementation of this if anyone else
was interested.

Alternately does anyone have another way of achieving similar things?

Nic Ferrier

xml mailing list, project page
xml gnome org


Steve Ball            |   XSLT Standard Library   | Training & Seminars
Zveno Pty Ltd         |     Web Tcl Complete      |   XML XSL Schemas |      TclXML TclDOM        | Tcl, Web Development
Steve Ball zveno com  +---------------------------+---------------------
Ph. +61 2 6242 4099   |   Mobile (0413) 594 462   | Fax +61 2 6242 4099

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]