Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Lee 1123 posts 3059 karma points
    Jan 13, 2010 @ 17:29

    Dynamic / Conditional Sorting?

    If I do ever get my head round why we use this XSLT I'll buy everyone a beer... But as I can't I'm sorry no Beers for now!  

    Yet again (Can you sense my frustration) something that should be simple, is insanely complicated with XSLT... How on earth do you get round conditional sorting?  Say based on QueryString value..

    For example, I want the users to be able to choose how to sort some data either via date or via a nodename or another property - The value is then posted as a QueryString.

    I have tried <choose> using a $variable but still it doesn't work??  Choose says invalid XSLT and if I stuff it in a variable it just doesn't work!?

    Please can someone put me out of my misery and show me how you get round conditional sorting? :(

  • Chris Houston 533 posts 978 karma points MVP admin c-trib
    Jan 13, 2010 @ 17:49
    Chris Houston

    Hi Lee,

    Have a look at this post here:

    Which should help you.



  • Chriztian Steinmeier 2731 posts 8338 karma points MVP 5x admin c-trib
    Jan 13, 2010 @ 20:31
    Chriztian Steinmeier

    Hi Lee,

    The problem is (as you may or may not have found out) that it's not allowed to use a variable or parameter in the select attribute of a sort instruction, so the way to get around it is to use a choose statement and test for all the possible values:

    <?xml version="1.0" encoding="utf-8" ?>
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
        <xsl:param name="currentPage" />
        <xsl:variable name="sortby" select="umbraco.library:RequestQueryString('sortby')" />
        <xsl:variable name="nodes" select="$currentPage/node" />
        <xsl:template match="/">
                <!-- Sort by name? -->
                <xsl:when test="$sortby = 'name'">
                    <xsl:apply-templates select="$nodes">
                        <xsl:sort select="@nodeName" data-type="text" order="ascending" />
                <!-- Sort by id? -->
                <xsl:when test="$sortby = 'id'">
                    <xsl:apply-templates select="$nodes">
                        <xsl:sort select="@id" data-type="number" order="ascending" />
                <!-- More ways to sort... -->
                <!-- Just take the Umbraco Content order then... -->
                    <xsl:apply-templates select="$nodes" />
        <!-- Template for how a single node should be output -->
        <xsl:template match="node">
                Name: <xsl:value-of select="@nodeName" />, Id: <xsl:value-of select="@id" />
        <!-- No output for hidden nodes -->
        <xsl:template match="node[data[@alias = 'umbracoNaviHide'] = 1]" />


    PS: I hope you get to like XSLT some day, 'coz I loooove beer :-)

  • Lee 1123 posts 3059 karma points
    Jan 13, 2010 @ 21:11

    Thanks for the help chaps, very, very appreciated :)

    @ Chris - I actually found that page, but couldn't quite get my head round the Xpath in the sort select=""?  I understand what she is saying and tried this

       <xsl:sort select="./data [@alias = 'distance']" data-type="number" />
            <xsl:sort select="./data [@alias='eventDate'][$resultsort = 'date']" data-type="text" order="ascending" />

    But it didn't do anything :S   Do I just add the condition after the XPath?  And is this right?  I want to sort on this

    ./data [@alias='eventDate']

    So added the following condition afterwards, which based on that ladies code should say if the variable $resultsort is equal to 'date' then apply the sort?

    [$resultsort = 'date']

    Which gives me

    ./data [@alias='eventDate'][$resultsort = 'date']

    @ Chriztian - Thanks for the code, I'll have a good look at it in a minute and see if I can use it if I can't get the XSLt working based on the link Chris gave.  The problem I have is the XSLT is quite complex already and I'm worried about adding all that extra code :(   But if thats the only way to do it, then so be it ;)   If it works, yet another person I'll owe beer too :)

  • Lee 1123 posts 3059 karma points
    Jan 13, 2010 @ 21:40


    Thanks for the help again everyone, I used idea from the link Chris put up and think I have it working now

    Pretty fiddly, will double check in the morning when I have had a break from this  

    Thanks again :)

  • Chriztian Steinmeier 2731 posts 8338 karma points MVP 5x admin c-trib
    Jan 13, 2010 @ 21:54
    Chriztian Steinmeier

    Yeah - I had a "Weee" moment too - the template with the choose an' all in my example can actually be written llike this, using Jeni's trick from the link above:

       <xsl:template match="/">
            <xsl:apply-templates select="$nodes">
                <xsl:sort select="@nodeName[$sortby = 'name']" data-type="text" order="ascending" />
                <xsl:sort select="@id[$sortby = 'id']" data-type="number" order="ascending" />


  • Lee 1123 posts 3059 karma points
    Jan 14, 2010 @ 07:29

    Hey Chriztian, thats 99% the same way I have done it on the page - But my sorts are for a number or by a date and the date is a DocType property

    <xsl:for-each select="$searchResults">
              <xsl:sort select="umbraco.library:FormatDateTime(./data [@alias = 'eventDate'] [string($resultsort) = 'date'], 'yyyyMMdd')" order="ascending" />
              <xsl:sort select="./data [@alias = 'distance'] [string($resultsort) = 'distance']" data-type="number" />

    Hope this thread helps someone else

  • kentiler 76 posts 69 karma points
    Feb 15, 2010 @ 18:26

    I'm having a tough time with this.  I have a property named sortOrder which is a numeric type.  When I put this inside my for-each loop,

     <xsl:sort select="./data [@alias = 'sortOrder'] [string($resultsort) = 'sortOder']" data-type="number" />

    But when I put this line in, I get this error:

    Error occured

    System.Xml.Xsl.XslLoadException: The variable or parameter 'resultsort' is either not defined or it is out of scope. An error occurred at D:\www\\html\xslt\634018299623240000_temp.xslt(25,3).
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
    at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

    Any ideas?  I thought ResultSort is a variable created by the sort.



  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 18:42
    Nik Wahlberg

    Hi Kent, can you post your entire XSLT here? resultsort would need to be defined in the your XSLT manually. With the rest of your file, I should be able to help out. 

    Off the bat, if you are simply tring to sort by that property it should look like this:

    <xsl:sort select="data [@alias = 'sortOrder']" data-type="number" />


  • organic 108 posts 157 karma points
    Dec 16, 2011 @ 19:46

    I'm trying to do the same task, sort based on a column if that coumn is specifed in the Url as the 'sort' querystring, but it's not working. The basic sort on 'lastName' works if I remove:


     Any ideas?

    <xsl:variable name="sortby"select="umbraco.library:RequestQueryString('sort')"/>

    <xsl:template match="/">

    <!--The fun starts here -->
    <table border="1" cellspacing="0" cellpadding="2">
    <tr class="gridHeader">
    <td><b><a href="?sort=number">#</a></b></td>
    <td><b><a href="?sort=lastName">Player</a></b></td>
    <td><b><a href="?sort=position">Position</a></b></td>
    <xsl:for-each select="$currentPage/
    node [string(data [@alias='hide'])!='1']">
    <xsl:sort select="
    data [@alias='lastName'][string($sortby)='lastName']" data-type="text" order="ascending" />
Please Sign in or register to post replies

Write your reply to: