tags:

views:

118

answers:

2

I have some data which i output in a for-each loop in xslt. I have paging working on the list, but not the sort selector.

The user should be able to sort on 2 values (created data, and a number field on each item). The default sort method is the create date, but when the user clicks "Sort by number" the list should instead order by a number value.

But the does not seem to accept a varialbe ($mySort) in the select statement - any ideas as to how i would go about this?

+1  A: 

If you can't get a variable to work for the order attribute you may have to do it the hard way. Something like:

<xsl:when test="$mySort = 'ascending'">
  <xsl:apply-templates>
    <xsl:sort order="ascending"/>
  </xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
  <xsl:apply-templates>
    <xsl:sort order="descending"/>
  </xsl:apply-templates>
</xsl:when>
</xsl:otherwise>
dkackman
Yeh, i saw that solution elsewhere - the problem with that is that it will mess up my paging script horribly :-(Should a variable for the sort work, or?
cJockey
+2  A: 
<xsl:sort select="*[name() = $mySort]" order="{$myOrder}" />

The select expression must be a valid, literal XPath expression. XPath cannot be dynamically evaluated in XSLT, which means that a variable containing an XPath string will not work.

However, the sort attribute accepts a string, this is why you can use an attribute value template (curly brackets expression) here.

Tomalak