Designs for XSLT functions (Was: Re: [xsl] RE: syntax sugar for call-template)

Subject: Designs for XSLT functions (Was: Re: [xsl] RE: syntax sugar for call-template)
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sun, 18 Feb 2001 09:41:06 +0000
Hello everyone,

There seems to be a reasonable amount of support for user-defined
functions written in XSLT, whether to sweeten the syntax of
xsl:call-template or to allow XPaths previously only dreamed about.

If we're going to move ahead with this, we need to agree on a syntax
for (1) declaring the functions and (2) calling the functions.  In
this email, I'm going to lay out the major designs that have been
suggested so far so that we can discuss them and hopefully come up
with some kind of resolution that's acceptable to everyone.

If I leave out an argument or you have a strong feeling in support or
contradiction of one, please comment on it. After we've discussed the
different options for a bit, I'll organise a vote and then put
together a proposal based on that. So long as no one objects to me
doing so?

Of course, we can discuss this as much as we want - if we don't get
implementation support then it's pointless. But I have faith that if
we come up with a well-thought-out proposal then at least some
implementers will come on board.

[Within this email, I'm using 'exsl' as a namespace prefix for a set
of common extensions, to highlight that new elements will have to be
defined somewhere and that they very likely won't make it into XSLT
1.1.]

1. Declaring Functions
----------------------

1.a. Using xsl:template vs. using exsl:function

Using xsl:template:

<xsl:template name="my:func">
  ...
</xsl:template>

Using exsl:function:

<exsl:function name="my:func">
  ...
</exsl:function>

Arguments for xsl:template include:
i.  it already exists
ii. it might then be possible to have a template that was used both
    with normal xsl:call-template syntax and as a function (though
    it's arguable whether this is desirable).

Arguments for exsl:function include:
i.  the ability to constrain its content in ways that are necessary for
    extension functions
ii. it highlights the distinction between templates and functions.

---

1.b. Top-level declaration vs. declaration within xsl:script

Top-level declarations:

<exsl:function name="my:func">
  ...
</exsl:function>

Declaration within xsl:script:

<xsl:script language="exsl:xslt" implements-prefix="my">
   <exsl:function name="func">
      ...
   </exsl:function>
</xsl:script>

Arguments for top-level declarations include:
i.   no reliance on xsl:script, in case it doesn't make it to XSLT 1.1
ii.  if declared with xsl:template (see above), allows templates to be
     used as both templates and functions
iii. if declared with xsl:template (see above), prevents 'double
     declaration' of namespaces for functions (in xsl:template/@name
     and in xsl:script/@implements-prefix)

Arguments for declaration in xsl:script include:
i.  logical place for function declarations - mirrors declarations in
    other languages
ii. allows assignment of function namespace without assigning in
    individual function names

---
    
1.c. exsl:return and/or result tree fragments

exsl:return:

<exsl:function name="my:func">
   <exsl:return select="'foo'" />
</exsl:function>

[Note - similar syntax for exsl:return as xsl:variable etc. - set
through select attribute or content.]

result tree fragments:

<exsl:function name="my:func">
   <xsl:text>foo</xsl:text>
</exsl:function>

both:

<exsl:function name="my:func">
   <xsl:choose>
      <xsl:when test="$mytest">foo</xsl:when>
      <xsl:otherwise>
         <exsl:return select="'foo'" />
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>

Arguments for exsl:return include:
i. allows return of value types other than RTFs

Arguments for returning result tree fragments include:
i. gives easy shorthand for returning values other than node sets

Arguments for *only* exsl:return include:
i.  prevents result elements from 'disappearing' or being written to
    the result tree

2. Calling Functions
--------------------

2.a. exsl:function() vs. my:func()

exsl:function():

   foo[exsl:function('my:func')]

my:func():

   foo[my:func()]

Arguments for exsl:function() [or similar] include:
i. allows function names to be decided dynamically

Arguments for my:func() include:
i. follows syntax for other extension functions

---

2.b. Passing parameters by position vs. name

Passing parameters by position:

   my:func(foo, 'bar', 1)

Passing parameters by name:

   my:func('one', foo,
           'two', 'bar',
           'three', 1)

Arguments for passing parameters by position include:
i. follows syntax for other extension functions

Arguments for passing parameters by name include:
i.  follows syntax for xsl:call-template
ii. more easily allows multiple optional parameters

    
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread