xsl-list
[Top] [All Lists]

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

2007-03-27 08:52:45
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.)
e.g.
----
<sorts>
  <sort direction="ascending">column-1</sort>
  <sort direction="descending">column-2</sort>
</sorts>

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(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Angela Williams <Angela(_dot_)Williams(_at_)the401kcompany(_dot_)com>
Sent:     Tue, 27 Mar 2007 10:28:59 -0500
To:       <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
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:

<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[(_at_)order="1"]/>
    <xsl:variable name=sort2 select="sort[(_at_)order="2"]/>
    <xsl:variable name=sort3 select="sort[(_at_)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
  




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>
  • RE: [xsl] Dynamically define number of xsl:sort stmts using parameters, cknell <=