Re: [xsl] Efficient Recursive Algorithms in XSLT (Was: Re: Constructing X number of elements)

Subject: Re: [xsl] Efficient Recursive Algorithms in XSLT (Was: Re: Constructing X number of elements)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 21 Aug 2001 13:32:44 -0400
With respect,

We've given Ilkka the simple recursive solution. And Dimitre has reminded us that in the general case, there are also better approaches. But Ilkka's problem may not need to be solved in the general case.

For example, let's assume there will never be more than five stars.

<my:stars> <!-- you'll need to have declared the 'my' namespace -->
  <img src="star.gif" alt="first star"/>
  <img src="star.gif" alt="second star"/>
  <img src="star.gif" alt="third star"/>
  <img src="star.gif" alt="fourth star"/>
  <img src="star.gif" alt="fifth star"/>
</my:stars>

<xsl:variable name="stars" select="document('')/*/my:stars/img"/>

Then,
<xsl:template match="movie">
  ...
  <xsl:copy-of select="$stars[position() &lt;= @rating]" />
  ...
</xsl:template>

This is neat, efficient, does not rely on recursion with its ungainliness and potential performance hit (as if that were likely to be a problem at five levels), and is easily extended to handle, say, a maximum of seven or nine stars. The stars can even be different.

It's a refinement of Chris's solution.

Cheers,
Wendell

At 10:42 AM 8/21/01, Dimitre wrote:
This solution will crash some XSLT processors and is generally less efficient (both
in time and space) than another general approach for implementing recursive
algorithms....



====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


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



Current Thread