The usual solution, which covers simple cases, is
<xsl:sort select="*[name()=$sortkey]"/>
For more complex cases you need to either (a) use xx:evaluate(), or (b)
generate or modify the stylesheet source before its compiled.
Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
Ben Macaulay
Sent: 07 November 2002 19:34
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
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