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

Re: Choosing different sorts

Subject: Re: Choosing different sorts
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 15 Jul 2004 15:22:53 +0100
sort xsl
Hi Derek,

> I have tried, in my code to have:
>
>   <xsl:for-each select="file">
>      <xsl:choose>
>         <xsl:when test="$sort='design'"><xsl:sort
> select="name"/></xsl:when>
>         <xsl:when test="$sort='stat'"><xsl:sort
> select="status"/></xsl:when>
>         <xsl:otherwise><xsl:sort select="id"/></xsl:otherwise>   
>
>       </xsl:choose>

As has been pointed out to you, you can't have <xsl:sort> elements
wrapped in a <xsl:choose> -- they must be the first, direct, children
of <xsl:for-each>.

The difficulty, then, is to select a different thing to sort by
depending on the value of the $sort variable. You've been shown a
couple of ways to do this. Another way is:

  <xsl:for-each select="file">
    <xsl:sort select="name[$sort = 'design'] |
                      status[$sort = 'stat'] |
                      id[$sort != 'design' and $sort != 'stat']" />
    ...
  </xsl:for-each>

This selects a node-set containing the <name> element child of <file>,
but only if $sort is 'design', the <status> element, but only if $sort
is 'stat', and the <id> element, but only if $sort is neither 'design'
nor 'stat'. Since the three conditions are mutually exclusive, you end
up with a node-set containing the single node that you want.

The advantage of this method is that the paths that select the nodes
you want can be anything at all -- they don't all have to be children
of the nodes you're sorting, for example.

Of course, in XSLT 2.0, things become a lot clearer. You can use the
if expression in XPath 2.0 as follows:

  <xsl:for-each select="file">
    <xsl:sort select="if ($sort = 'design') then name
                      else if ($sort = 'stat') then status
                      else id" />
    ...
  </xsl:for-each>

Or if you prefer you can use an <xsl:choose> inside the <xsl:sort>, as
in:

  <xsl:for-each select="file">
    <xsl:sort>
      <xsl:choose>
        <xsl:when test="$sort = 'design'">
          <xsl:sequence select="name" />
        </xsl:when>
        <xsl:when test="$sort = 'stat'">
          <xsl:sequence select="status" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:sequence select="id" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:sort>
    ...
  </xsl:for-each>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

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.