xsl-list
[Top] [All Lists]

Dynamic Sorting

2002-11-07 12:34:07
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



<Prev in Thread] Current Thread [Next in Thread>