Re: [xsl] Paging and Sorting

Subject: Re: [xsl] Paging and Sorting
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 18 Oct 2001 16:19:23 +0100
Hi Katie,

> I have the paging functionality working, but not the sorting. What
> do I need to do to do to include sorting cababilities when the user
> clicks on a column link? My XSL and XML are below.

What you've got at the moment is:

> <xsl:if test="$column='LoanId'">
>         <xsl:apply-templates select="ReturnResultSet/LoanSearchSet">
>         <xsl:sort select="ReturnResultSet/LoanSearchSet/Loan/LoanId" 
> order="ascending"/>
>         </xsl:apply-templates>
> </xsl:if>
> <xsl:if test="$column='LastName'">
>         <xsl:apply-templates select="ReturnResultSet/LoanSearchSet">
>         <xsl:sort 
> select="ReturnResultSet/LoanSearchSet/Loan/LoanBorrowerSet/Borrower/LastName" 
> order="ascending"/>
>         </xsl:apply-templates>
> </xsl:if>

The reason this isn't working is that the select attribute of xsl:sort
is evaluated relative to the nodes that are selected by the
surrounding xsl:apply-templates/xsl:for-each. At the moment, the
paths are looking for the 'ReturnResultSet' children of the
'LoanSearchSet' nodes that are being sorted.

I think what you actually want to do is sort the Loans against the
LastName or LoanId, in which case you need to apply templates to the
Loan elements, as follows:

  <xsl:choose>
    <xsl:when test="$column = 'LoanId'">
      <xsl:apply-templates
          select="ReturnResultSet/LoanSearchSet/Loan">
        <xsl:sort select="LoanId" />
      </xsl:apply-templates>
    </xsl:when>
    <xsl:when test="$column = 'LastName'">
      <xsl:apply-templates
          select="ReturnResultSet/LoanSearchSet/Loan">
        <xsl:sort select="LoanBorrowerSet/Borrower/LastName" />
      </xsl:apply-templates>
    </xsl:when>
  </xsl:choose>

Alternatively, you could use the following trick:

  <xsl:apply-templates select="ReturnResultSet/LoanSearchSet/Loan">
    <xsl:sort select="LoanId[$column = 'LoanId'] |
                      LoanBorrowerSet[$column = 'LastName']
                        /Borrower/LastName" />
  </xsl:apply-templates>

If $column is 'LastName' then the LoanId element will be selected, but
the LoanBorrowerSet won't be, so you'll end up sorting by the value of
the LoanId element. On the other hand, if the $column is 'LastName',
then the LoanId element won't be selected but the
LoanBorrowerSet/Borrower/LastName element will be, so you'll end up
sorting by last name.

Another thing I'll just mention is that you're making very strange use
of attribute value templates. You only need to put {}s around
expressions in attributes that you want to be evaluated dynamically
and inserted into the attribute value. If you're adding a static
string to the attribute value, then there's no need to use an
attribute value template. For example, instead of:

> <a
> href="/loans/SearchResults.jsp?{'sort='}{$column}{'&amp;upperLimit='}{$upperLimit}{'&amp;action=First'}{'&amp;counter='}{$counter}">
> <b>First Page</b></a>

You could more simply have:

  <a
  href="/loans/SearchResults.jsp?sort={$column}&amp;upperLimit={$upperLimit}&amp;action=First&amp;counter={$counter}">
  <b>First Page</b></a>

I hope that helps,

Jeni

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


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread