Re: [xsl] generic sort based on attribute names

Subject: Re: [xsl] generic sort based on attribute names
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 23 Jan 2005 21:05:59 +1100
In XSLT 2.0 + FXSL one could write the following:

 <xsl:perform-sort select="/*/e">
   <xsl:sort select=
                  f:xsltSort( f:map(f:name(), f:attributes(.)),

and this produces the desired result:

<e z="zz" a="aa"/>
<e b="bb" y="yy"/>

This can probably be written as a single XPath expression using nested
f:xsltSort(), but this would be not too easy to understand.

f:xsltSort() is an interesting function, based on xsl:perform-sort:

 <xsl:function name="f:xsltSort" as="item()*">
   <xsl:param name="pSeq" as="item()*"/>
   <xsl:param name="pCriteria" as="node()*"/>
   <xsl:perform-sort select="$pSeq">
     <xsl:sort select="f:compose-flist($pCriteria, .)"/>

It is an XPath shorthand of xsl:perform-sort, with the peculiarity,
that the functions to be used as sort keys must be passed in reverse
order (composition takes place from right to left).

Also, it is not possible to define a function for any arbitrary
expression on the fly -- this would have been possible if we had the
facility to specify Lambda functions in XPath, but as we know, no
function definition is possible within XPath.

Dimitre Novatchev.

On Sat, 22 Jan 2005 19:23:21 -0800, Chaitanya Desai <cdesai@xxxxxxxxxxx> wrote:
> Hi,
> I am trying to write a generic sort that uses the 'concatenated
> name-value pairs of all attributes sorted' as the key to sort elements
> at a particular level.
> Example
> Suppose
> <root>
> <e b="bb" y="yy"/>
> <e z="zz" a="aa"/>
> </root>
> is the XML I want to sort.
> The result of the sort should be
> <root>
>  <e a="aa" z="zz" />
>  <e b="bb" y="yy" />
> </root>
> Thus the attributes within an element are sorted and then the key used
> for sorting elements would be:
> 'az' and 'by' respectively (thus 'az' < 'by').
> I am able to sort attributes within an element.
> But I am having trouble setting the key to sort for elements.
> Any help would be greatly appreciated.
> Thanks
> Chaitanya

Current Thread