Dynamic sort in XSLT?
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开发者_JS百科 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?
<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.
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>
精彩评论