[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Conditional xsl:sort
Hi David, > I have the following: > > ... > <xsl:for-each select="Transaction"> > <xsl:sort select="*[local-name() = string($sortby)]" > data-type="{$datatype}" order="{$order}"/> > > <xsl:if test="$sortby = 'Description'"> > <xsl:sort select="Code" data-type="number" order="{$order}"/> > </xsl:if> > ... > > and I'm getting this: > > ... > Keyword xsl:sort may not be used here. > ... > > for the second, conditional, xsl:sort. Why? Because xsl:sort cannot be used within an xsl:if. There are only two elements in which xsl:sort is legal in XSLT 1.0 -- within xsl:apply-templates and at the start of a xsl:for-each. To do conditional sorts you have to be a bit cunning. For the second sort, create an expression that only selects the Code element if the $sortby variable has the value 'Description', and use that: <xsl:for-each select="Transaction"> <xsl:sort select="*[local-name() = string($sortby)]" data-type="{$datatype}" order="{$order}" /> <xsl:sort select="Code[$sortby = 'Description']" data-type="number" order="{$order}" /> ... </xsl:for-each> If the $sortby variable isn't 'Description' then Code[$sortby = 'Description'] won't select any nodes, so all the Transactions will be sorted by the empty string (the same value) for the second sort and it will have no effect. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|