Copied to clipboard

Flag this post as spam?

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


  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 14:00
    Jonas Eriksson
    0

    Limit number of items in an For-each with if-expression inside

    Hi!

    This is on the edge of my xslt-knowledge... I like to limit the numbers of items in an for-each with if-expression inside. Can someone point out some directions / examples would be very helpful.

    The thing is that I like to limit the number of items in a <ul> <li> - list, I can not use the position()-function since I have if-expressions, like so_

    <xsl:for-each select="...node [expressions]">
    <xsl:sort...>
    <xsl:if test="[more expressions]">
    <xsl:if test="show only first $maxItems">
    <li> .... </li>

    Perhaps I should create a nodelist with for-each and do a for-each on the new list. Dunno how really.

    Thanks alot!

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jan 06, 2010 @ 14:14
    Douglas Robar
    1

    Why not combine your if statements with the 'and' operator?

    <xsl:if test="[more expressions] and position() &lt; $maxItems">

    If that's not going to work for you, feel free to give us a longer snippet of your code so we can understand what you're trying to achieve.

    cheers,
    doug.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jan 06, 2010 @ 15:00
    Nik Wahlberg
    0

    Hi Jonas, please post your code here so that we may review. postion() should do what you need (if I understand your snippet correctly) and what Doug suggested should do it. 

    Thanks,
    Nik

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 16:07
    Jonas Eriksson
    0

    Thank you for your inputs.

    Doug, the think is that the expressions got so complex so I felt the need to put some of the logic outside the for-each. Nik, the position works only on the for-each-items as I understand. But since I have the If-expression inside the for-each-loop the position does not work. Yes, a code snippet should clarify things, I'll tidy up and get back soon...

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 16:22
    Jonas Eriksson
    0

    Here's an example

    <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
      <hr/>
      Position outside if:<xsl:value-of select="position()"/><br/>
      <xsl:if test="position() mod 2=0">
        Position inside if:<xsl:value-of select="position()"/><br/>
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
        </a>
      </xsl:if>
    </xsl:for-each> 

    The result is something like this:

    --------------------------------------------------------------------------------
    Position outside if:1
    --------------------------------------------------------------------------------
    Position outside if:2
    Position inside if: 2
    Second node name
    --------------------------------------------------------------------------------
    Position outside if:3
    --------------------------------------------------------------------------------
    Position outside if:4
    Position inside if: 4
    Fourth node name
    --------------------------------------------------------------------------------
    Position outside if:5
    --------------------------------------------------------------------------------
    Position outside if:6
    Position inside if: 6
    Sixth node name

    And I need something like this:

    --------------------------------------------------------------------------------
    Position outside if:1
    --------------------------------------------------------------------------------
    Position outside if:2
    Position inside if: 1
    Second node name
    --------------------------------------------------------------------------------
    Position outside if:3
    --------------------------------------------------------------------------------
    Position outside if:4
    Position inside if: 2
    Fourth node name
    --------------------------------------------------------------------------------
    Position outside if:5
    --------------------------------------------------------------------------------
    Position outside if:6
    Position inside if: 3
    Sixth node name
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 06, 2010 @ 16:52
    Lee Kelleher
    0

    Hi Jonas,

    It's a bit of a cowboy hack, but try this:

    Position inside if: <xsl:value-of select="position() div 2" /><br/>

    ... or is that TOO much cowboy? ;-)

    Cheers, Lee.

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 17:03
    Jonas Eriksson
    1

    Sweet, this works:

    <xsl:variable name="firstForEachList">
      <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
        <xsl:if test="position() mod 2=0">
          <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:value-of select="@nodeName"/>
          </a>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:for-each select="msxsl:node-set($firstForEachList)/a [position() &lt; 3]">
      <xsl:value-of select="position()"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
     
  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 17:09
    Jonas Eriksson
    0

    Lee, thanks for that shot from the hip :) however, the expression mod 2 was just an example and the real one is more complex and hard to calculate with. The macro is all about listing media nodes with permissions. The permissions are set with references from the media folders to content nodes. A bit hackish indeed.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jan 06, 2010 @ 17:39
    Nik Wahlberg
    0

    Glad to see you worked it out. Position was your friend after-all :) 

    Cheers,
    Nik

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 06, 2010 @ 17:43
    Lee Kelleher
    0

    No worries Jonas, I had a feeling it wasn't what you needed!  Glad that you've got it working now! (If only you could mark your own posts as solutions?)

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 06, 2010 @ 18:30
    Jonas Eriksson
    0

    Yes, thanks guys, position() combined with msxsl:node-set() was the key.

Please Sign in or register to post replies

Write your reply to:

Draft