[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Conditional xsl:sort

Subject: Re: Conditional xsl:sort
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 13 Jun 2002 15:49:47 +0100
xsl if sortby
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.