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

Subject: Re: Designs for XSLT functions (Was: Re: [xsl] RE: syntax sugar for call-template)
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Feb 2001 14:50:01 +0000
Hi Clark,

> Not a very good example... but then again, I was trying to think of
> a syntax that would allow a node set to be built-up over the course
> of the function, and <exsl:return ... /> does not do this.

I did come up with a use case that I think touches on what you're
talking about here, namely returning the first five nodes in an
alphabetical sort of a node set.

<xsl:variable select="my:first-alphabetically($nodes, 5)" />

Under your scheme I think you would use:

<exsl:function name="my:first-alphabetically">
   <exsl:param name="nodes" />
   <exsl:param name="number" />
   <xsl:for-each select="$nodes">
      <xsl:sort />
      <xsl:if test="position() &lt;= $number">
         <exsl:append select="." />
      </xsl:if>
   </xsl:for-each>
</exsl:function>

Without something like exsl:append, you would need to use a recursive
solution which is longer and uglier, but it could be done:

<exsl:function name="my:first-alphabetically">
   <exsl:param name="nodes" />
   <exsl:param name="number" />
   <xsl:choose>
      <xsl:when test="$number = 1">
         <xsl:for-each select="$nodes">
            <xsl:sort />
            <xsl:if test="position() = 1">
               <exsl:result select="." />
            </xsl:if>
         </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
         <xsl:variable name="first"
                       select="my:first-alphabetically($nodes, 1)" />
         <xsl:variable name="rest"
                       select="$nodes[count(.|$first) != 1]" />
         <exsl:result select="$first |
                              my:first-alphabetically($rest,
                                                      $number - 1)" />
      </xsl:otherwise>
   </xsl:choose>
</exsl:function>

I *think* that if you need to build up a node set gradually then it's
always possible to use a recursive solution to do it, but I could be
wrong.  I'd like to see a use case that couldn't be solved
recursively, so where exsl:append or exsl:reference-of would be
necessary.

Cheers,

Jeni

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



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


Current Thread