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

Subject: RE: [xsl] Dynamically define number of xsl:sort stmts using parameters
From: cknell@xxxxxxxxxx
Date: Tue, 27 Mar 2007 11:52:16 -0400
I think you just may have disturbed the "no side-effects" troll. If I were in your position, I would use a two-stage approach. That is to say, use a stylesheet to create a stylesheet that does the transformation.

1) Create an XML file with your sort conditions. (Do your users check off the columns they want to sort by in some user interface? Then attach a function to create the XML document to the user interface widget that tells the program to perform the sort.)
  <sort direction="ascending">column-1</sort>
  <sort direction="descending">column-2</sort>

2) Create a stylesheet that transforms this document into the stylesheet that will perform the sort.

3) Apply the XSLT-created stylesheet to the data document.

4) You're done.
Charles Knell
cknell@xxxxxxxxxx - email

-----Original Message-----
From:     Angela Williams <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Sent:     Tue, 27 Mar 2007 10:28:59 -0500
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  [xsl] Dynamically define number of xsl:sort stmts using parameters

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:

  <!-- more address nodes -->

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

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:  

    <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 -->

    <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 -->

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?

Angela Williams
Channel Developer
The 401k Company

Current Thread