Re: [xsl] RE: syntax sugar for call-template

Subject: Re: [xsl] RE: syntax sugar for call-template
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 16 Feb 2001 19:40:45 +0000
Hi Mike,

> Calling a template from a function within an XPath expression is not
> a good idea. It's important that functions should have no
> side-effects, and templates normally have the side-effect of writing
> to the result tree. That's why I made saxon:function a distinct
> construct from xsl:template.

I'm not sure I understand (or rather I'm sure I don't understand).
Could you explain a bit more?

Isn't the very fact that the template is being called in the context
of an XPath expression enough to indicate that its contents aren't
going to be written to the result tree? The same sort of thing happens
with templates that are called within variables:

<xsl:variable name="matches">
   <xsl:template name="match">
      <xsl:with-param name="string" select="$string" />
      <xsl:with-param name="regexp" select="$regexp" />

The result of the call to the template isn't written to the result
tree immediately, but assigned to the variable.

Given a call-template() function (or equivalent), couldn't we say:

   The template is called with the specified parameters. If the
   template exits with an xsl:return element then the result of the
   call-template() is the value returned from the template. Otherwise,
   the result of the call-template() is a new document with a root
   node having as its children the sequence of nodes that results from
   instantiating the template.

The effect of this would be that normal templates (as we have in XSLT
1.0) would always return RTFs (by any other name). If a template
included an xsl:return, then it would normally return the value that
specified.  If you had something like:

<xsl:template name="func">
      <xsl:when test="$mytest">do this</xsl:when>
         <xsl:text>don't do this</xsl:text>
         <xsl:return select="'do that'" />

then if $mytest was true the template would return a RTF (containing a
text node with a value of 'do this').  If $mytest is false then it
would return a string with a value of 'do that'.  It would be
equivalent to:

<xsl:template name="func">
      <xsl:when test="$mytest">
         <xsl:variable name="x">do this</xsl:variable>
         <xsl:return select="$x" />
         <xsl:text>don't do this</xsl:text>
         <xsl:return select="'do that'" />

If we were using xsl:script, then placing a template within that
construct could mark it out as a function (i.e. it would be exactly as
if it had been declared with saxon:function instead).

I thought you might be referring to the problem that the template
could contain things like xsl:message or xsl:document. I can (just
about) imagine XPaths that could contain a call to a template that
created a document, which was then accessed with a call to document().
Although looking at saxon:function, I see that xsl:document is
apparently acceptable! :)

As usual I have no clue about the implementation implications of the
above and await education expectantly.



Jeni Tennison

 XSL-List info and archive:

Current Thread