XSLT: use parameters in xls:sort attributes (dynamic sorting)
- by fireeyedboy
How do I apply a parameter to a select and order attribute in a xsl:sort element? I'ld like to do this dynamic with PHP with something like this:
$xsl = new XSLTProcessor();
$xslDoc = new DOMDocument();
$xslDoc->load( $this->_xslFilePath );
$xsl->importStyleSheet( $xslDoc );
$xsl->setParameter( '', 'sortBy', 'viewCount' );
$xsl->setParameter( '', 'order', 'descending' );
But I'ld first have to now how to get this to work. I tried the following, but it gives me a 'compilation error' : 'invalid value $order for order'. $sortBy doesn't seem to do anything either:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="sortBy" select="viewCount"/>
<xsl:param name="order" select="descending"/>
<xsl:template match="/">
<media>
<xsl:for-each select="media/medium">
<xsl:sort select="$sortBy" order="$order"/>
// <someoutput>
</xsl:for-each>
</media>
</xsl:template>
</xsl:stylesheet>