Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 945 posts 2077 karma points
    Nov 11, 2010 @ 00:48
    Nigel Wilson
    0

    Suming and sorting of nodes

    Hey 

    Just looking for direction / advice:

    I am creating a photo website that runs online competitions. There is to be voting for the competitions that I intend storing within fields on the node. So the pseudo data structure will look like:

    <image>
        <judge1>10</judge1>
        <judge2>6</judge2>
        <judge3>8</judge3>
    </image>

    Initially I was intending to add another field (totalScore) and have an event handler to loop through and perform the calculation and store the total. The resulting XSLT output could then be easily sorted by the totalScore field.

    But then I thought there might be a way of dynamically calculating the totals instead, but in doing so it seems that the sorting part is somewhat harder.

    I prefer the latter solution as it keeps things more simple - it also reduces the potential for bugs in my code and an XSLT is easier to fix than compiled code. :-)

    However I guess dynamically calculating totals on "page load" will incur some performance loss. At this stage the number of images is approx. 100.

    Umbraco experts - I would appreciate your views on the best way to achieve this? 

    With regards

    Nigel

  • Nigel Wilson 945 posts 2077 karma points
    Nov 15, 2010 @ 08:44
    Nigel Wilson
    0

    For future refernce / anyone stumbling across this post I have figured out a solution to the above.

    100% credit goes to the Douglas Robar and his fantastic XSLT Search package. I recalled seing some of his code detail the sorting of search results so used this logic to sort the nodes based on 3 separate fields.

    The solution I have implemented uses some inline c# code to perform the sum calculations.

    My code is as follows:

    <xsl:variable name="mediaFolderID" select="$currentPage/mediaFolder"/>
    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($mediaFolderID, true())"/>

    <xsl:variable name="nodesWithTotals">
    <xsl:text>;</xsl:text>

    <xsl:for-each select="$mediaItems/competitionsImage">
    <!-- unique id for this node -->
    <xsl:value-of select="generate-id(.)"/>

    <xsl:text>=</xsl:text>

    <!-- total score for the matches -->
    <xsl:value-of select="yy:sumScores(number(./judgeScore1),number(./judgeScore2),number(./judgeScore3))"/>

    <xsl:text>;</xsl:text>
    </xsl:for-each>
    </xsl:variable>

    <xsl:for-each select="$mediaItems/competitionsImage">

    <xsl:sort select="substring-before(substring-after($nodesWithTotals, concat(';',generate-id(.),'=')),';')" data-type="number" order="descending"/>

    //Your code goes here to output the images . . .

    </xsl:for-each>

    The c# code that goes just inside the closing </xsl:stylesheet> tag

        <msxml:script language="C#" implements-prefix="yy">
            <![CDATA[
            public int sumScores(int score1, int score2, int score3) {
                   
                int scoreSum = score1 + score2 + score3;
                return scoreSum;
               
             }
             ]]>
        </msxml:script>

    Also in the opening <xsl:stylehseet> tag I added the following

    xmlns:yy="urn:schemas-yoyocms-co-nz" 

    and in the exclusions I added the "yy" bit !

    exclude-result-prefixes="msxml yy umbraco.library"

    I hope this is clear enough - feel free to get in touch if you find this and want to discuss. I feel like I am able to finally give something back to the Umbraco community.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft