[xsl] Conditional xsl:sort and leaving some items out of sorting

Subject: [xsl] Conditional xsl:sort and leaving some items out of sorting
From: Abel Braaksma Online <abel.online@xxxxxxxxx>
Date: Wed, 12 Jul 2006 12:27:48 +0200
Hi List!

I have some trouble with "xsl:sort" in XSLT 1.0. FYI, it must run on a client IE, FF, Op etc, using Msxml2.XSLTemplate.5.0 on MS IE and XSLTProcessor() on others. I am well aware of the shortcomings of this approach. Most of the time, sticking strictly to XSLT 1.0 specs gets me a long way.

The logical thing I am trying to achieve is the following (order by cell[1] if $name variable is not equal to 'generic'):

  <xsl:apply-templates select="twz:rows/twz:row" >
      <xsl:if test="$name != 'generic'">
          <xsl:sort select="twz:cell[1]" order="ascending" />
      </xsl:if>
  </xsl:apply-templates>

But this is illegal syntax, as xsl:sort cannot be a child of xsl:if. It doesn't trigger an error either, by the way. I came up with this brute force solution:

  <xsl:if test="$name = 'generic'">
      <xsl:apply-templates select="twz:rows/twz:row" />
  </xsl:if>
  <xsl:if test="$name != 'generic'">
      <xsl:apply-templates select="twz:rows/twz:row" >
          <xsl:sort select="twz:cell[1]" order="ascending" />
      </xsl:apply-templates>
  </xsl:if>

But actually I hoped to be able to do something like this eventually:

<xsl:apply-templates select="twz:rows/twz:row" >
<xsl:sort select="($name = 'generic' and position()) or twz:cell[1]" order="ascending" />
</xsl:apply-templates>



Is my approach the "xslt way" of doing things, or is there a different way of doing this? Is there a pattern available about this? In addition, I would like to ask how to extend the solution above to order the list in such a way that all is ordered except items that have the value "None" or "All". These should end up on the bottom of the list.


Any help or pointers would be appreciated. I tried Michael Kays books, but these emphasize on XSLT 2.0 and I couldn't find how to do this better than above.

Cheers,
Abel Braaksma
Nuntia B.V.
www.nuntia.nl (online July 17)

Current Thread