Copied to clipboard

Flag this post as spam?

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


  • kukuwka 75 posts 96 karma points
    Oct 06, 2010 @ 14:14
    kukuwka
    0

    How to get number of items in for-each

    Hi ,

    I want to know how many items fit to 'WHEN":

    <xsl:variable name="count" select="1>
     
    <xsl:for-each select="$items">
     
    <xsl:choose>
    <xsl:when test="somthing">

        <xsl:variable name="count">
               <xsl:value-of select="$count+1"/>

     </xsl:variable>

    </xsl:when>
    </xsl:choose>

    </xsl:for-each>

    but when I do:

    <xsl:value-of select="$count"/> I get alwayse 1.


    How can I get right number of items?

    Thanks,

  • Jeff Grine 149 posts 189 karma points
    Oct 06, 2010 @ 15:07
    Jeff Grine
    1

    You can't reassign to a variable in xslt. Looks like your best bet would be to include your condition in the variable and use the count function on that. Something like

    <xsl:variable name="itemsWhereSomeCondition" select="$items[condition='something']"/>
    <xsl:variable name="count" select="count($itemsWhereSomeCondition)"/>

     

  • Rik Helsen 670 posts 873 karma points
    Oct 06, 2010 @ 16:58
    Rik Helsen
    0

    Use the position function, it offers quite a few extras:

     

    In this example we check if the current item (this is within a foreach loop) is the last item.

           <xsl:if test="position()=last()">
                 <xsl:attribute name="class">last</xsl:attribute>
            </xsl:if>
        <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">         <xsl:attribute name="class">active</xsl:attribute>                 <xsl:if test="position()=last()">                      <xsl:attribute name="class">active last</xsl:attribute>                 </xsl:if>     </xsl:if>

    if the current position is smaller than "5"

    <xsl:for-each select="$currentPage/descendant::node">
    <xsl:if test="position() &lt;= 5">
    </xsl:if>
    </xsl:for-each>
    

    These examples are for the old XML scheme, but the position function works the same...

  • Magnus Eriksson 122 posts 362 karma points
    Oct 06, 2010 @ 19:10
    Magnus Eriksson
    0

    The problem with using position() is that it isn't conditional, it would increase even if the xsl:when didn't match, so if it has to be conditional I would look at count().

    Regards,
    Magnus

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Oct 06, 2010 @ 19:50
    Nik Wahlberg
    0

    If you absolutely need to have a 'counter' then you may want to consider an XSLT Extension that could do this for you. What are you trying to accomplish? There may be another way to solve your issue if we know more...

    Thanks,
    Nik

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 06, 2010 @ 20:51
    Chriztian Steinmeier
    0

    Hi kukuwka,

    For the way you state your problem, Jeff's suggestion is the way to go - it looks like you just want to count how many of $items fit the condition, and you can literally tag the condition on to that as a predicate (i.e., in square brackets) and wrap the count() function around that; e.g., to count how many of $items that have the value "Royal Blue" in the property "penColor":

    <xsl:value-of select="count($items[penColor = 'Royal Blue'])" />

    /Chriztian

  • kukuwka 75 posts 96 karma points
    Oct 10, 2010 @ 12:28
    kukuwka
    0

    Thank you for all replies.

    I want somethind like:

    <xsl:for-each select="$items">
    <xsl:variable name="content">
      <xsl:choose>
       <xsl:when>
           something
      </xsl:when>

       <xsl:when>
           something
       </xsl:when>

        <xsl:when>
           something
     </xsl:when>

     </xsl:choose>
    </xsl:variable>

    <xsl:choose>

     </xsl:for-each>

     

    <xsl:when test="string-length($content) &gt; 0">
      
    here count++

    </xsl:when>

    </xsl:choose>

Please Sign in or register to post replies

Write your reply to:

Draft