Copied to clipboard

Flag this post as spam?

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


  • sdfsfm 70 posts 94 karma points
    Aug 05, 2010 @ 18:58
    sdfsfm
    0

    correct way of sorting in xslt

    I'm trying to sort a news section by date order but its not working, I want the latest entry at the top. Is this the correct way of sorting an item?

     <xsl:sort select="@sortOrder" data-type="number" order="descending" />

    or this way?

    <xsl:for-each select="$currentPage/ancestor-or-self::root//* [name() = $documentTypeAlias and string(umbracoNaviHide) != '1']">
    <!-- <xsl:sort select="@sortOrder" data-type="number" order="descending" />-->

    <!-- newsDate alias of date picker-->
    <xsl:sort select="newsDate"  order="ascending" />

                <xsl:if test="position() = 1 ">
                    <article>
                        <h2>
                            <a href="{umbraco.library:NiceUrl(@id)}" >
                                <xsl:value-of select="@nodeName"/>
                            </a>
                        </h2>
                        <p class="date">
                            <xsl:value-of select="umbraco.library:FormatDateTime(./newsDate, 'd MMMM, yyyy')"/>
                        </p>
                        <p>
                            <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(./newsBodyText), 100,'')"  />
                        </p>
                        <p class="bg-greenBtn">
                            <a href="{umbraco.library:NiceUrl(@id)}" title="" >
                                <span>Continue reading</span>
                            </a>
                        </p>
                    </article>
                </xsl:if>
            </xsl:for-each>
  • Steen Tøttrup 191 posts 291 karma points c-trib
    Aug 05, 2010 @ 19:07
    Steen Tøttrup
    1

    I'm pretty sure the date in the xml isn't a number, but a string, so the question is whether or not you can do a sort on that string and expect it to come out in the right order. Unless the date always looks like this: yyyy-mm-dd you really can't sort it in the correct order.

    So this is the way:

    <xsl:sort select="newsDate"  order="ascending" />
  • Bas Schouten 135 posts 233 karma points
    Aug 05, 2010 @ 19:10
    Bas Schouten
    0

    I think you have to use: <xsl:sort select="data[@alias='newsDate']" order="ascending" />

  • Steen Tøttrup 191 posts 291 karma points c-trib
    Aug 05, 2010 @ 19:14
    Steen Tøttrup
    0

    Well, that really depends on which version he's using, but yes, that might be with correct if it's not Umbraco 4.5.x

  • sdfsfm 70 posts 94 karma points
    Aug 05, 2010 @ 19:22
    sdfsfm
    0

    @Bas I'm using 4.5 so it woudln't work on the old schema.

    @Steen  I'm using

    <xsl:sort select="newsDate"  order="ascending" />

    But it doesn't work, no matter what date I set to an item, it always appends the list last.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Aug 05, 2010 @ 20:45
    Kim Andersen
    0

    What will happen if you try this one:

    <xsl:sort data-type="text" select="./newsDate" order="descending" />

    Using text as the data type.

    /Kim A

  • Sascha Wolter 615 posts 1101 karma points
    Aug 05, 2010 @ 21:16
    Sascha Wolter
    0

    Couple of ideas:

    - Have you tried just sorting by @nodeName for instance to see if it has any effect?

    - I tend to convert the dates explicitly to dates for comparison like so (have never used the data-type parameter though):

    <xsl:sort select="Exslt.ExsltDatesAndTimes:date(./newsDate)" order="descending" />
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 06, 2010 @ 00:27
    Chriztian Steinmeier
    0

    Hi ivorthedesigner,

    To clarify a couple of things:

    Dates in the Umbraco XML are indeed strings, not numbers, but they are sortable because of the format (believe it's called 'xmlDate' or similar), so this will work:

    <xsl:sort select="newsDate" order="descending" />

    (The data-type attribute defaults to "text" so it isn't necessary)

    You are using "Date Picker" or "Date Picker with time" as the Type for the newsDate property, right? 

    I can't help but notice that while you say you want the newest items at the top, all the examples (save for Kim's) are using "ascending" as the order???

    Did you try using @nodeName as Sascha suggested, just to be sure that you're actually selecting the right nodes?

    /Chriztian

     

  • sdfsfm 70 posts 94 karma points
    Aug 06, 2010 @ 10:16
    sdfsfm
    0

    Hi All thanks for the replies.

    Unfortunately due to my stupidity, I was putting on the wrong macro!! No wonder it didn't work.|I had two macro's one showing only the latest item, and one showing all the news items.

    @Sascha  & @Steen Thanks for all your help your solutions worked.

    @Chriztian Thanks for the explanation, it much clearer in my head whats happening now then before. Yes I did know the order was ascending, I changed it to descending in my code.

Please Sign in or register to post replies

Write your reply to:

Draft