Copied to clipboard

Flag this post as spam?

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


  • jaihind 4 posts 24 karma points
    Sep 15, 2012 @ 20:36
    jaihind
    0

    Unable to get the count of conditional results from a for-each loop outside the loop

    Hi,

    I'm trying to get the count of the records that satisfy my specific conditions within a for-each loop and use that count for processing outside the loop.

    How to do it?

    Please let me know if anybody knows it.

    Thanks in advance.

     

    Regards,

    Jaihind

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Sep 15, 2012 @ 23:21
    Jan Skovgaard
    1

    Hi Jaihind and welcome to our :)

    Could you please post a sample of the code you're trying to get working?

    It should not be neccesary to do a for-each loop in xslt in order to count specific nodes since you should be able to simply do something like <xsl:value-of select="count($currentPage/Textpage)" />

    Looking forward to hearing more from you.

    /Jan

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 16, 2012 @ 12:40
    Chriztian Steinmeier
    0

     

    Hi Jaihind,

    Because variables in XSLT are really just constants (they're designed for stuff that may change from transformation to transformation - not changing while transforming) you cannot re-assign or change a variable once it's set. That may seem like a Catch-22 when you can't declare a variable before a for-each, and then set its value inside, to use it back out on the other side again. But the thing is - if you can get the value inside the for-each, you can also get it outside.

    So if, for instance, you need to know how many pages were processed, to display some kind of counter - cache the pages in a variable before the loop (to avoid executing the XPath twice) and iterate the variable for the loop, then use the count() afterwards, e.g.:

     

    <xsl:variable name="subPages" select="$currentPage/Textpage[not(umbracoNaviHide = 1)]" />
    
    <h2>Pages</h2>
    
    <xsl:for-each select="$subPages">
        <p><xsl:value-of select="@nodeName" /></p>
    </xsl:for-each>
    
    <hr />
    (<xsl:value-of select="count($subPages)" /> displayed)
    
    But as Jan says - let's see some of the stuff you're trying, so we can give you some pointers on how to achieve that.

    /Chriztian

  • jaihind 4 posts 24 karma points
    Sep 17, 2012 @ 09:22
    jaihind
    0

    Hi,

    I'm trying to apply the logic wherein I am trying to reduce the count on the basis of particular condition and use it further.

    Code is like this - 

    <xsl:variable name="totalCount"><xsl:numbervalue="count(//node/dubnode)" />xsl:variable>

    < xsl:for-eachselect="//node/subnode">

    <xsl:iftest="condition 1 and condition ''">

    --reduce the count by one

     <xsl:if>

    <xsl:for-each>

    I'm trying to use that modified count after the for loop.

    As we cannot reassign the value back to the variable, I'm looking for an alternative way.

     

    Regards,

    Jaihind

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 17, 2012 @ 09:56
    Chriztian Steinmeier
    0

    Hi Jaihind,

    If you're doing something like that, you can move that condition into a 'predicate' on the selection you're counting, e.g. to elaborate on my original sample:

    <xsl:variable name="subPages" select="$currentPage/Textpage[not(umbracoNaviHide = 1)]" />
    
    <h2>Pages</h2>
    
    <xsl:for-each select="$subPages">
        <xsl:if test="pancakes &gt; 5">
            <p><xsl:value-of select="@nodeName" /></p>
        </xsl:if>
    </xsl:for-each>
    
    <hr />
    (<xsl:value-of select="count($subPages[pancakes &gt; 5])" /> displayed)

    /Chriztian

  • jaihind 4 posts 24 karma points
    Sep 17, 2012 @ 10:58
    jaihind
    0

    Thank you very much Chriztian..

    I was stuck because of this issue for last two days.

    This thing worked.

    Thanks once again.

     

    Regards,

    Jaihind

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Sep 17, 2012 @ 11:01
    Chriztian Steinmeier
    0

    Great!

    Henceforth let it be known that pancakes solves all our problems :-)

    /Chriztian

  • jaihind 4 posts 24 karma points
    Sep 17, 2012 @ 13:49
    jaihind
    0

    lol :)

  • 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