Copied to clipboard

Flag this post as spam?

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


  • Klaus Jensen 1 post 21 karma points
    Sep 13, 2009 @ 14:27
    Klaus Jensen
    0

    Basic sort/orderby

    Total xlst-n00b and first umbraco-project.

    Im using the CWS starter kit, and need to change the order of items displayed in SubNavi.xslt

    I believe the line in question, that needs to be altered is this:

    <xsl:variable name="rootTextpageNode" select="$currentPage/ancestor-or-self::node [@level = 2 and @nodeTypeAlias = 'CWS_Textpage']" />

    How can I put a sort/orderby in there, so items with newer createdDate are displayed first?

     

     

     

  • dandrayne 1138 posts 2262 karma points
    Sep 13, 2009 @ 14:53
    dandrayne
    0

    Hi There

    A sort needs to go inside the for-each loop, so find that and pop the sort just after the for-each loop opens.  Also see http://www.w3schools.com/xsl/el_sort.asp

    <xsl:sort select="whatToSortBy" data-type="text|number|qname" order="ascending|descending"/>

    Dan

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 13, 2009 @ 17:16
    Sebastiaan Janssen
    1

    Ha, I can get karma here AND on stack overflow now ;-)

    As said on SO:

    You can do a sort in the first line after a for-each, like so:

    <xsl:for-each select="$rootTextpageNode">
    <xsl:sort select="@createDate" order="descending" />
    <xsl:value-of select="@nodeName" />
    </xsl:for-each>

     

  • Ove Andersen 435 posts 1541 karma points c-trib
    Sep 14, 2009 @ 11:35
    Ove Andersen
    0

    If you want to customize sorting, take a look at the sorting method in Warren Buckley's CWS.

    In short: you can edit your own document types or macro to include the properties "sortBy" and "sortOrder".
    To make it easier for later use, you can add two custom datatypes called "Sort By List" and "Sort Order List"

    Add the following values to "Sort By List": sortOrder, createDate, updateDate.
    Add the following values to "Sort Order List": ascending, descending.

    Now you have a datatype you can use in your document types. I have these properties added to my document types that has a lot of subnodes under it. In my XSLT, I then look for these properties and list the subnodes accordingly.

    In your XSLT, add this at the top of your template:

     <xsl:variable name="SortOrder">
      <xsl:choose>
       <xsl:when test="$source/node/data [@alias = 'sortOrder'] != ''">
        <xsl:value-of select="$source/node/data [@alias = 'sortOrder']" />
       </xsl:when>
       <xsl:otherwise>
        <xsl:text>ascending</xsl:text>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:variable>
     <xsl:variable name="SortBy">
      <xsl:choose>
       <xsl:when test="$source/node/data [@alias = 'sortBy'] != ''">
        <xsl:value-of select="$source/node/data [@alias = 'sortBy']" />
       </xsl:when>
       <xsl:otherwise>
        <xsl:text>sortOrder</xsl:text>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:variable>
     <xsl:variable name="DataType">
      <xsl:choose>
       <xsl:when test="$SortBy='sortOrder'">
        <xsl:value-of select="'number'" />
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="'text'" />
       </xsl:otherwise>
      </xsl:choose>
     </xsl:variable>

    Then, directly under your for-each, add the following:

    <xsl:sort select="@*[name() = $SortBy]" order="{$SortOrder}" data-type="{$DataType}"/>

    Hope this helps!

Please Sign in or register to post replies

Write your reply to:

Draft