This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Conditional xsl:sort
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: "David B. Bitton" <david at codenoevil dot com>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 13 Jun 2002 15:49:47 +0100
- Subject: Re: [xsl] Conditional xsl:sort
- Organization: Jeni Tennison Consulting Ltd
- References: <021101c212e5$200192e0$0601030a@codenoevil.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi David,
> I have the following:
>
> ...
> <xsl:for-each select="Transaction">
> <xsl:sort select="*[local-name() = string($sortby)]"
> data-type="{$datatype}" order="{$order}"/>
>
> <xsl:if test="$sortby = 'Description'">
> <xsl:sort select="Code" data-type="number" order="{$order}"/>
> </xsl:if>
> ...
>
> and I'm getting this:
>
> ...
> Keyword xsl:sort may not be used here.
> ...
>
> for the second, conditional, xsl:sort. Why?
Because xsl:sort cannot be used within an xsl:if. There are only two
elements in which xsl:sort is legal in XSLT 1.0 -- within
xsl:apply-templates and at the start of a xsl:for-each.
To do conditional sorts you have to be a bit cunning. For the second
sort, create an expression that only selects the Code element if the
$sortby variable has the value 'Description', and use that:
<xsl:for-each select="Transaction">
<xsl:sort select="*[local-name() = string($sortby)]"
data-type="{$datatype}" order="{$order}" />
<xsl:sort select="Code[$sortby = 'Description']"
data-type="number" order="{$order}" />
...
</xsl:for-each>
If the $sortby variable isn't 'Description' then Code[$sortby =
'Description'] won't select any nodes, so all the Transactions will be
sorted by the empty string (the same value) for the second sort and it
will have no effect.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list