RE: [xsl] Dynamic Sorting

Subject: RE: [xsl] Dynamic Sorting
From: "Américo Albuquerque" <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Thu, 7 Nov 2002 20:25:22 -0000
Hi Ben.
You can, but you'll have to change a little the sort.
With te variable defined like this:
 <xsl:variable name="sort-by-element-1" select="'surname'"/>
 <xsl:variable name="sort-by-element-2" select="'forename'"/>

You can sort the nodes like this:
  <xsl:apply-templates>
   <xsl:sort select="*[name()=$sort-by-element-1]"/>
   <xsl:sort select="*[name()=$sort-by-element-2]"/>
  </xsl:apply-templates>

Hope that this helps you

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Ben Macaulay
Sent: Thursday, November 07, 2002 7:34 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Dynamic Sorting


Question -> Can you define a template that allows you to define, at
runtime, the elements that you want to sort on?  For instance, we have
an XML document as follows.

    <records>
        <contact id="0001">
            <title>Mr</title>
            <forename>John</forename>
            <surname>Smith</surname>
        </contact>
        <contact id="0002">
            <title>Dr</title>
            <forename>Amy</forename>
            <surname>Jones</surname>
        </contact>
        ...
    <records>

What we want to do is sort by (surname, forename), or sort by (title,
surname), or whatever we decide on the day.  In our XSL, we would like
to be able to specify variables that dictate how the sorting should be
done.  For instance, consider the following XSL extract.

    <xsl:variable name="sort-by-element-1" value="surname" />
    <xsl:variable name="sort-by-element-2" value="forename" />

In reality we are not going to hard code the variable values like this,
but rather pull them in from another document by way of the document()
function, but that's not important right now.

The problem is, later on in the XSL we can't have <xsl:sort
select="{$sort-by-element-1}" /> since xsl:sort's select attribute
doesn't accept attribute value templates.  You can specify attribute
value templates for xsl:sort's other attributes, for instance the
article at
http://www.biglist.com/lists/xsl-list/archives/200012/msg00392.html
demonstrates how to dynamically specify a descending or ascending sort
order.  However you can't do this for the select attribute.  Short of
creating nonportable XSL by using saxon:evaluate() or xalan:evaluate() I
can't see a way around this.  Any suggestions?  Can this kind of dynamic
sorting be done at all?


Cheers,
Ben Macaulay
Senior Software Engineer
DHL Worldwide Express

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



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


Current Thread