Copied to clipboard

Flag this post as spam?

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


  • Sean Parsons 23 posts 38 karma points
    Mar 05, 2010 @ 22:05
    Sean Parsons
    0

    Limiting output to a certain number of nodes

    Hi all, I'm sure a variation of this has been asked before but I haven't found the exact solution yet.

    I need to limit the output of my nodes in a macro based firstly on date (this I can do successfully) but would only like 4 items output.

    In other words, there may be 9 items in the list, the first 2 and the 4th may be out of date so I'd like to see the 3rd, 5th, 6th and 7th in the list.

    So for the following XML

    <list>
        <item1 date="1 Mar 2010">1</a>
        <item2 date="2 Mar 2010">2</a>
        <item3 date="10 Mar 2010">3</a>
        <item4 date="4 Mar 2010">4</a>
        <item5 date="13 Mar 2010">5</a>
        <item6 date="14 Mar 2010">6</a>
        <item7 date="18 Mar 2010">7</a>
        <item8 date="21 Mar 2010">8</a>
        <item9 date="31 Mar 2010">9</a>
    </list>

    I'd expect to see

    <output>
        <item3 date="10 Mar 2010">3</a>
        <item5 date="13 Mar 2010">5</a>
        <item6 date="14 Mar 2010">6</a>
        <item7 date="18 Mar 2010">7</a>
    </output>

    Sure it's an easy one for an experienced XSLT coder. (Don't worry about the date code - I have that completed already but I am getting a list that's still more than 4 items long)

    Thanks
    Sean

  • Stephan Lonntorp 195 posts 212 karma points
    Mar 05, 2010 @ 23:02
    Stephan Lonntorp
    2
    <xsl:if test="position() &lt;= 4">...</xsl.if>
  • Sean Parsons 23 posts 38 karma points
    Mar 05, 2010 @ 23:10
    Sean Parsons
    0

    Will this not give the position of the node in scope? What if that node is to be excluded based on the date? Will it still be counted or will the position() refer to the position of the nodes added in to the output??

     

    I have a feeling that position() will report the position of the node in scope...

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 06, 2010 @ 00:50
    Chriztian Steinmeier
    1

    Hi Sean,

    Your feeling is right, part of the way. Normally, in XSLT, if you have a template for an element, and you use the apply-templates instruction to output these, you'd get the position of the node in the original XML document when using position(), even if you use the sort instruction to reorder the elements. The for-each instruction is your friend here, because it will change the context and give you the new position() values.

    So you want to use for-each to select the nodes in question and sort them by date, then test the value of position() before applying templates - something like this pseudo-code:

    <xsl:template match="/">
        <xsl:for-each select="/path/to/list/item">
            <xsl:sort select="@date" data-type="text" order="descending" />
            <xsl:if test="position() &lt;= 4">
                <xsl:apply-templates select="." />
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="item">
        <!-- Output for item -->
    </xsl:template>

    Hope it helps,

    /Chriztian

  • Sean Parsons 23 posts 38 karma points
    Mar 09, 2010 @ 20:39
    Sean Parsons
    0

    Hi Chriztian - thanks for the reply and apologies for the delay in replying myself.

    The above situation that you stated makes sense, but I'm still a bit confused about how to limit the OUTPUT number of nodes to 4. My exact context is that I have a number of News nodes - and I only wish to display the current (i.e. news relevant to today or later) nodes on the page.

    So I will need an AND statement in my <xsl:for-each... for example (pseudo-code)

    <xsl:template match="/">
    <xsl:for-each select="$currentPage//node[@nodeTypeAlias = 'News']" AND "umbraco.library:DateGreaterThanOrEqualToday(current()/data [@alias='newsDate'])">
    <xsl:sort select="data" order="ascending" />
    <xsl:if test="position() &lt;= 4">
    OUTPUT HERE
    etc...

    Anad that's not possible, as far as I'm aware.

    Does that make more sense of what I am trying to achieve?

    Thanks,
    Sean

  • Peter Dijksterhuis 1442 posts 1722 karma points
    Mar 09, 2010 @ 20:46
    Peter Dijksterhuis
    0

    Hi Sean,

    yes you can do this, try this:

    <xsl:for-each select="$currentPage//node[@nodeTypeAlias = 'News' and umbraco.library:DateGreaterThanOrEqualToday(data [@alias='newsDate'])]">

    HTH,

    Peter

  • Sean Parsons 23 posts 38 karma points
    Mar 10, 2010 @ 21:52
    Sean Parsons
    0

    Perfect - thanks Peter, and the others who got me there eventually.

Please Sign in or register to post replies

Write your reply to:

Draft