Copied to clipboard

Flag this post as spam?

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


  • Martyn Joyce 6 posts 26 karma points
    Nov 02, 2010 @ 18:35
    Martyn Joyce
    0

    Looping all site nodes and sorting the results...

    Hi guys,

    In short my aim is to show a top 10 list of the latest selected articles from my site on the homepage.

    I have added a datatype to each article called 'showOnHomepage' and now i want to search all of these and select the 10 latest ones.

    I have searched around and have some code used for generating site maps which is finding all of the pages, but I dont know how to sort the results.

    My Code so far...

    <xsl:template match="/">
      <xsl:apply-templates select="$currentPage/*[@isDoc and (self::article)]" />
    </xsl:template>

    <xsl:template match="*[@isDoc]">
      <xsl:if test="showOnHomepage = 1">

        <!-- write out the id of the matching doc -->
        <xsl:value-of select="@id" />

      </xsl:if>
      <xsl:if test="*[@isDoc]">
        <xsl:apply-templates select="*[@isDoc]" />
      </xsl:if>
    </xsl:template>


    any help would be great!

    Cheers

    Martz

  • Almir Vereget 62 posts 150 karma points
    Nov 02, 2010 @ 21:56
    Almir Vereget
    0

    Hi, 

    If i understand it right, you can sort them by updateDate. Something like this:

    <xsl:apply-templates select="$currentPage/*[@isDoc]">
        <xsl:sort select="@updateDate" order="descending"/>
    </xsl:apply-templates>

    Cheers!

  • Martyn Joyce 6 posts 26 karma points
    Nov 03, 2010 @ 09:34
    Martyn Joyce
    0

    excellent thanks, that seems to work.... templates (and xslt for that matter) are all a bit new to me :-)

  • Martyn Joyce 6 posts 26 karma points
    Nov 06, 2010 @ 15:04
    Martyn Joyce
    0

    Looks like I jumped the gun a little here...

    The sort xsl does work for the current xsl template but the code moves on and searches the next level independantly and so these results aren't merged and sorted.

    i.e. all level 1 nodes get sorted correctly, but sub folders get sorted and added to the end of the level 1 list.

    Perhaps there is an easier way to go about this whole issue?

    I would also like to only show the top 10 matching nodes but this method of searching doesnt lend itself well to doing this either!

  • Almir Vereget 62 posts 150 karma points
    Nov 07, 2010 @ 11:41
    Almir Vereget
    0

    OK, is this is doing any good:

        <xsl:template match="/">
            <xsl:apply-templates select="$currentPage//*[@isDoc and (self::article)]">
                <xsl:sort select="@updateDate" order="descending"/>
            </xsl:apply-templates>
        </xsl:template>
    
        <xsl:template match="*[@isDoc]">
            <xsl:if test="showOnHomepage = 1 and position() &lt;= 10">
                <!-- write out the id of the matching doc -->
                <xsl:value-of select="@id" />
            </xsl:if>
        </xsl:template>
    
  • Martyn Joyce 6 posts 26 karma points
    Nov 08, 2010 @ 16:07
    Martyn Joyce
    0

    Yes that works like a dream, thanks very much.

    Which part of this makes it search the whole site... is it the '//' in the following section:

    $currentPage//*[@isDoc and (self::article)]

    as I havent seen that before?

    Thanks again

     

     

  • Martyn Joyce 6 posts 26 karma points
    Nov 08, 2010 @ 16:22
    Martyn Joyce
    0

    Infact i spoke to soon again! the top 10 section isnt working correctly as position() is just looking at the top 10 articles whether they match to showOnHomepage or not. It needs to be the top 10 that also have showOnHomepage= 1

    for example, if I set the position for top 5 instead of 10 (for ease) and have the following list of articles:

    article 1 - showOnHomepage = 1
    article 2 - showOnHomepage = 0
    article 3 - showOnHomepage = 0
    article 4 - showOnHomepage = 0
    article 5 - showOnHomepage = 1
    article 6 - showOnHomepage = 1
    article 7 - showOnHomepage = 1

    The code will only show articles 1 and 5, as 6 and 7 are after position 5.

  • Almir Vereget 62 posts 150 karma points
    Nov 08, 2010 @ 22:44
    Almir Vereget
    0

    Hi Martyn,

    yes "//" is abbreviated syntax for "descendant-or-self::".

    You can try something like this:

    <xsl:template match="/">
            <xsl:for-each select="$currentPage//*[@isDoc and (self::article) and showOnHomepage = 1]">
                <xsl:sort select="@updateDate" order="descending"/>
                <xsl:if test="position() &lt;= 10">
                    <!-- write out the id of the matching doc -->
                    <xsl:value-of select="@id" />
                </xsl:if>
            </xsl:for-each>
        </xsl:template>

    As you can see i'm not using match templates here any more, instead i'm using <xsl:for-each> to loop through content.

    This is more "using XSLT as traditional programming language", and not the "best" way but like majority i learned XSLT that way, and i'm not too familiar in writing XSLT using match templates.

    Hope it helps.

  • Martyn Joyce 6 posts 26 karma points
    Nov 09, 2010 @ 18:14
    Martyn Joyce
    0

    thanks again Almir, thats the way I normally approach xsl and makes much more sense to me... I didn't know there was a descendant-or-self option, learn something new every day.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft