This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Conditional xsl:sort


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]