XSLT: use parameters in xls:sort attributes (dynamic sorting)

Posted by fireeyedboy on Stack Overflow See other posts from Stack Overflow or by fireeyedboy
Published on 2010-03-27T13:41:25Z Indexed on 2010/03/27 14:33 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

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>

© Stack Overflow or respective owner

Related posts about xslt

Related posts about php