Copied to clipboard

Flag this post as spam?

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


  • bayshield 50 posts 65 karma points
    Mar 01, 2010 @ 13:22
    bayshield
    0

    Select top 10 ordered by child node count

    I am trying to write some XSLT to select the top 10 nodes based on the number of child nodes, however I am struggling to get this to work.  Does anyone have an similar examples I could look at?

    For example if I have 100 categories and each category has 1 or more products, how do I get the top 10 categories (based on the num of products they contain).

     

    Thanks

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 01, 2010 @ 13:30
    Chriztian Steinmeier
    3

    Hi,

    The basic selection could be done by sorting with the count() function, e.g., if the subpages of currentPage are the categories: 

       <xsl:template match="/">
            <xsl:for-each select="$currentPage/node[@nodeTypeAlias = 'Category']">
                <xsl:sort select="count(node)" data-type="number" order="descending" />
                <xsl:if test="position() &lt;= 10">
                    ...
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    

    /Chriztian

  • Osman Coskun 170 posts 404 karma points
    Dec 03, 2013 @ 22:23
    Osman Coskun
    0

    I want to order blog posts by children count by razor. Any ideas?

    TIA

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 03, 2013 @ 23:31
    Dennis Aaen
    0

    Hi TIA,

    Maybe this could be an inspiration for you to find the right solution.

      @foreach (var item in Model.BlogPosts.Where("Visible").OrderBy("CreateDate desc").Take(10))
        {
        <li><a href="@item.Url">@item.Name</a>
            <p>
                @item.Summary
            </p>

        </li>
    }

    In the example above, I get all the children where the document type is BlogPost, and the .Where("Visible") means that I only shows the blopost that not have the UmbracoNaviHide set to true.

    Then I order the blogposts by their creating dates, so the newest is alway on the top. and the Take makes you possible to difine how many items you want to display. In your case you want to dispay 10, so therefor Take(10).

    @item.Summary references to a property on the document type for a BlogPost, and then I also make a link to the page, and the link text is the name of the item in the content tree

    I hope this can help you getting what you needed, and make sense.

    /Dennis

  • Osman Coskun 170 posts 404 karma points
    Dec 10, 2013 @ 17:25
    Osman Coskun
    0

    What i want to make is sort the blog posts by comment count, then take the top 3 post as popular posts.

    The logic could be something like creating and array then adding the posts & child count to the array. Then sorting array by child count. And lastly take desired amount of posts and do whatever you want.

    I'm not an advanced razor coder. I'll try the above method in a free time.

Please Sign in or register to post replies

Write your reply to:

Draft