[xsl] Dynamically define number of xsl:sort stmts using parameters

Subject: [xsl] Dynamically define number of xsl:sort stmts using parameters
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Tue, 27 Mar 2007 10:28:59 -0500
Good morning, list -

My requirement is that I have an unknown number of sort conditions that I want
to apply to a for-each, for-each-group, or apply-templates construct.

Is there a way to dynamically define the number of <xsl:sort> statements to
use based on selecting parameters from an xml?

For example, <list> might have 0 to n <sort/> nodes:

<list>
  <address>
    <name/>
    <street/>
    <city/>
  </address>
  <!-- more address nodes -->

  <sort order="1">name</sort>
  <sort order="2">city</sort>
<!-- might have more sort nodes -->
</list>

If I have no nodes, I would want to omit the <xsl:sort> statement, etc...

I can think of the following theoretical solutions, but I don't know if they
are even possible:

1. (I know this doesn't work) Create multiple variables to evaluate the sort
value and then hardcode the same number of <xsl:sort> statements that may be
evaluating a null value for @select:

  <snip>
    <xsl:variable name=sort1 select="sort[@order="1"]/>
    <xsl:variable name=sort2 select="sort[@order="2"]/>
    <xsl:variable name=sort3 select="sort[@order="3"]/> <!-- empty -->
  </snip>

  <snip>
    <xsl:for-each select="address">
       <xsl:sort select="saxon:evaluate($sort1)/>
       <xsl:sort select="saxon:evaluate($sort2)/>
       <xsl:sort select="saxon:evaluate($sort3)/>  <!-- error here -->
    </xsl:for-each>
  </snip>

2. Use xsl to select the sort nodes and then dynamically write and output the
template and for-each loop that uses the appropriate number of sort statements
- then how would I call it in the same stylesheet?

3. Write multiple templates using 0 to n sort statements and then call the
appropriate template based on the number of <sort/> nodes found?  This doesn't
sound very elegant or practical to me, but might be the simplest...

Any suggestions?


Thanks!
Angela Williams
Channel Developer
The 401k Company
512-344-1547
 

Current Thread