Search code examples
xmlsortingxsltif-statementxsl-choose

xsl:sort does not work together with xsl:choose or if


Im going crazy on this XSL problem i have!

The thing is that i want to sort newspaper after what is choosen in a FORM. If $sort_newspaper = 'name' and it should sort after name (<xsl:sort select="./@name"/> )... but... it does not work if the xsl:sort exist inside the choose or after. It also does not work with xsl:if.

To be clear it work like the code are now, the choose works...

    <xsl:for-each select="./newspaper[count(. | key('newspaper_key', ./@id)[1]) = 1]">
            <xsl:sort select="./@name"/> 

            <xsl:choose>
                <xsl:when  test="$sort_newspaper = 'name'">
                    XSL:SORT SHOULD BE HERE BUT THAT WILL RESULT IN ERROR!
                </xsl:when>
                <xsl:otherwise>
                    HALLO
                </xsl:otherwise>
            </xsl:choose>
IF XSL:SORT WOULD BE HERE IT WOULD RESULT IN ERROR TOO!
    </xsl:for-each>

Solution

  • Sorry bad news. This will not work. Only possible solution (I see at the moment) would be to put the whole xsl:for-each into the xsl:when (with or without sort).

    Your code example should than look like this:

    <xsl:variable name="newspaper_group" select="./newspaper[count(. | key('newspaper_key', ./@id)[1]) = 1]" />
    <xsl:choose>
        <xsl:when  test="$sort_newspaper = 'name'">
            <xsl:for-each select="$newspaper_group" >
                <xsl:sort select="./@name"/>
                <!-- Sorted stuff -->
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$newspaper_group" >
                <!-- Unsorted stuff -->
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>