Copied to clipboard

Flag this post as spam?

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


  • Phil Crowe 192 posts 256 karma points
    Jun 07, 2010 @ 12:07
    Phil Crowe
    0

    Show just the top 2

    how can i display just the first two records in an xslt loop? Im wanting to display just the latest 2 news stories on my homepage.

     

     

    <xsl:for-each select="$currentPage/descendant-or-self::node[(@nodeTypeAlias = 'NewsStory')]">

    <h2><xsl:value-of select="@nodeName"/></h2>

    <p><xsl:value-of select="data[@alias='IntroPara']" disable-output-escaping="yes"/></p>

    </xsl:for-each>

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Jun 07, 2010 @ 12:11
    Lee Kelleher
    1

    Hi Phil,

    There are 2 ways to do it, first by limiting the number of nodes that are used in the for-each loop:

    <xsl:for-each select="$currentPage/descendant-or-self::node[@nodeTypeAlias = 'NewsStory' and position() &lt;= 2]">

    But the problem with that is they aren't sorted, it will only take the first 2 nodes - which "might" not be in the correct date order.

    So your best bet is to go with an IF condition within the loop...

    <xsl:for-each select="$currentPage/descendant-or-self::node[(@nodeTypeAlias = 'NewsStory')]">
        <xsl:sort select="@createDate" order="descending" />
        <xsl:if test="position() &lt;= 2">
            <h2>
                <xsl:value-of select="@nodeName"/>
            </h2>
            <p>
                <xsl:value-of select="data[@alias='IntroPara']" disable-output-escaping="yes"/>
            </p>
        </xsl:if>
    </xsl:for-each>

    Cheers, Lee.

  • Phil Crowe 192 posts 256 karma points
    Jun 07, 2010 @ 12:16
    Phil Crowe
    0

    Thanks a lot Lee 

  • Stefan Kip 1606 posts 4098 karma points c-trib
    Jun 07, 2010 @ 13:15
    Stefan Kip
    0

    This is a perfect example of the limitations of XSLT :P

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies