Re: [xsl] dynamically generate stylesheet parts

Subject: Re: [xsl] dynamically generate stylesheet parts
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Thu, 28 Mar 2002 06:57:35 -0800
On Thursday 28 March 2002 05:34, robert.soesemann@xxxxxx wrote:
> sort rows with column 3 as first key, 4 as second key and so on...
>
> This would map to this in an XSL:
>
> ...
> <xsl:sort select="column[@nr=3]" />
> <xsl:sort select="column[@nr=4]" />
> <xsl:sort select="column[@nr=7]" />
> ...
>
> My problem is, that I don't know how many of those <xsl:sort are needed.
> So I need to generate this part of my stylesheet dynamically.
>
> Can I use <xsl-call-template> and <xsl:text> or is this not possible?

Not possible.  You have a few options, though:

1) Dynamically generate your stylesheet (ie., a stylesheet that creates the 
stylesheet that is actually used for the processing).  Any application that 
is not entirely static (where the stylesheets are run only at build-time) 
shouldn't do this, for obvious performance reasons.

2) Generate a set of stylesheets with all possible combinations.  This 
wouldn't have to be all possible combinations of the <xsl:sort>s, but only 
combinations of the number of <xsl:sort>s that are present.  For example, 
have one stylesheet that has:

<xsl:param name="$sort-1"/>
...
<xsl:sort select="column[@nr=$sort-1]"/>

And another that has:

<xsl:param name="$sort-1"/>
<xsl:param name="$sort-2"/>
...
<xsl:sort select="column[@nr=$sort-1]"/>
<xsl:sort select="column[@nr=$sort-2]"/>

And so on.  Then take the $sort-{1,2,...} values from the "?sort=3,4,7".

Then when it comes time to process a request, make your Servlet or whatever 
else you use look at the number of sort parameters and pick the appropriate 
stylesheet from the cache.  It would be pretty easy to write a script to 
create this cache of stylesheets, or even to generate a new stylesheet the 
first time a request is made with a given number of sorts.  This is the route 
I would recommend, as it still allows preparsing of the stylesheets (or even 
compiling with XSLTC) and the overhead of picking the one with the right 
number of <xsl:sort>s is minimal.

3) Pick some maximum number of <xsl:sort>s that you are going to support.  
Then have all of the <xsl:sort>s in the same stylesheet, pass the values as 
parameters, and have some "default" sort that will not affect the outcome of 
the other sorts.  For example:

<xsl:param name="$sort-1" select="'this will never happen'"/>
<xsl:param name="$sort-2" select="'this will never happen'"/>
.. <!-- "N" is the maximum number of params that you will support -->
<xsl:param name="$sort-N" select="'this will never happen'"/>
...
<xsl:sort select="column[@nr=$sort-1]"/>
<xsl:sort select="column[@nr=$sort-2]"/>
..
<xsl:sort select="column[@nr=$sort-N]"/>

Say that only $sort-1 and $sort-2 are specified.  Then $sort-3...$sort-N will 
use the default sort key of "this will never happen", and hopefully there are 
no <column nr="this will never happen"/>.  That way, the unspecified sort 
keys do not affect the result of the other sort keys.

This is the easiest solution, since it can be done using only a single 
stylesheet.  It also would greatly simplify things if you have more than one 
situation like this to worry about in the same stylesheet.  But it would 
affect runtime performance, since each unspecified $sort-X would have some 
overhead.

Hope that all made, sense -- it's definitely time for bed over here :)

-- 
Peter Davis
Men seldom show dimples to girls who have pimples.

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


Current Thread