Copied to clipboard

Flag this post as spam?

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


  • Ben Deavin 3 posts 23 karma points
    Jun 20, 2011 @ 17:53
    Ben Deavin
    0

    Sorting by a function to ignore empty field

    Hi,

    This may be quite an xslt noob question and I don't know if this is possible but I'd like to sort a list of conted by date created but then have an optional 'DatePosted' field that will override the date created.

    So at the moment we use

    <xsl:sort select="@createDate" order="descending" />

    but I want something like

    <xsl:sort select="displayDateFunc(page)" order="descending" />

    function displayDateFunc(page){

    if(page.DatePosted != ""){
    return page.DatePosted
    } else {
    return page.CreationDate
    }
    }

    obviously that won't work (being psudo-javascript and not xslt) but is there a way I could do that sort of thing? and if so how?

    Thanks for your help

     

    Ben

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jun 20, 2011 @ 19:48
    Kim Andersen
    1

    Hi Ben

    So you want to sort your items by DatePosted if this one is present otherwise use the @createDate, is that correct?

    I found this WIKI entry that shows you how this can be achieved. The below piece of code is taken from that entry with some changes to your alias-names:

    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' ]">
     
    <xsl:sort  select="./DatePosted [string(current()/DatePosted)!=''] | ./@createDate [string(current()/DatePosted)='']"order="descending"/>
     
    <!-- Do stuff -->
    </xsl:for-each>

    I hope this can help you out.

    /Kim A

  • Ben Deavin 3 posts 23 karma points
    Jun 22, 2011 @ 18:31
    Ben Deavin
    0

    Thanks, that looks like what I'm trying to achieve but unfortunately it doesn't seem to work, it just ignores the DatePosted even if a value is present.  Any ideas?

    Thanks

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 22, 2011 @ 19:20
    Tom Fulton
    0

    Are you sure "DatePosted" is the right property alias and correct casing?  Usually Umbraco doesn't let you use a capital letter to start a property alias

  • Ben Deavin 3 posts 23 karma points
    Jun 22, 2011 @ 19:39
    Ben Deavin
    0

    Well it is actually datePosted, but I changed taht in the code already.  still doesn't word :-(

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jun 22, 2011 @ 23:03
    Kim Andersen
    0

    Did you change the alias name in all of the three places in the snippet above?

    Could you maybe try to print out the nodes in your for-each loop to check if the datePosted property is properly set. You can do this by printing out the nodes in a textarea like this:

    <textarea>
    <xsl:for-each
    select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1' ]">
     
    <xsl:copy-of select="." />
    </xsl:for-each>
    </textarea>

    /Kim A

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Jun 23, 2011 @ 01:00
    Chriztian Steinmeier
    2

    Hi Ben - this should work (I know, we all say that :-) :

    <xsl:sort select="(@createDate[not(normalize-space(../datePosted))] | datePosted)[1]" data-type="text" order="descending" />

    (Builds a set of either both dates or only the datePosted date - then selects the first in the set)

    /Chriztian

  • Kim Andersen 1447 posts 2196 karma points MVP
    Jun 23, 2011 @ 19:07
    Kim Andersen
    0

    Uhh, that was a new one Chriztian! Haven't seen something like that before, but I sure like that snippet :)

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft