Re: [xsl] Re:[xsl] Sort By calclulated variable

Subject: Re: [xsl] Re:[xsl] Sort By calclulated variable
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 4 Sep 2006 14:23:16 +0100
You need to sort on an expression that depends on the current item being
sorted, not on a variable (which will always have the same value on
every item)

In xslt1 it needs to be done in a single xpath (the same is true in
xslt2 but is not a restriction there as in xslt2 you can call arbitrary
xslt instructions from xpath)

so i'd guess that it is something like

<xsl:sort
select="sum(Price/Amount|$root[current()/Price/Amount]/Price/Amount|//Group[not(current()/Price/Amount)]/Price/Amount)"/>

Although //Group/Price/Amount/text() looks very odd // is a very
expensive operation, if you know where your Group element is, you should
give an explict path to it. Your version (with value-of) would just use
the first Group and silently discard any laterones, my version with
sum() would sum all the Group prices, i have no idea which you need. I
also left of the text() as it's usually a bad idea to have text() in
such an Xpath (it forces the system to do the wrong thing if there are
any comments in that element) In any case it is a constant value, so
perhaps you want
<xsl:variable name="default" select="/a/b/c/Group/Price/Amount)])"/>

...
<xsl:sort select="sum(Price/Amount|$root[current()/Price/Amount]/Price/Amount|$default[not(current()/Price/Amount)])"/>

David

Current Thread