Copied to clipboard

Flag this post as spam?

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


  • umbracocool 108 posts 197 karma points
    Oct 27, 2012 @ 10:14
    umbracocool
    0

    Counter or accumulator with XSLT For Each

    Hello friends I want to do is something like this:

    <xsl:variable name="counter" select="1"/>
     <xsl:for-each select="$nodes"
       <xsl:variable name="counter" select="$counter+1" />
     </xsl:for-each>
    <xsl:value-of select="$counter" />

    I want to get a battery that is accumulated current value plus one. I do not know because as complicated xslt in achieving this. Some alternative? Help please!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 27, 2012 @ 15:06
    Chriztian Steinmeier
    0

    Hi umbracocool,

    I'm not sure what you really trying to do, but the snippet you've posted can be done with a single line using the count() function:

    <xsl:value-of select="count($nodes) + 1" />

    You almost never need to "just" count in XSLT, because you'll most likely be doing it based on the data available, for which you have a lot of tools available. Problem is, that it's a different way of thinking if you're very used to traditional "procedural" programming...

    Let me know if your use case is more specific. 

    /Chriztian

  • umbracocool 108 posts 197 karma points
    Oct 27, 2012 @ 17:08
    umbracocool
    0

    Hola Chriztian!

    I want to get to have a counter of all the news, sports field that has the same soccer. Is this possible?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 27, 2012 @ 23:15
    Chriztian Steinmeier
    0

    Hola,

    I guess maybe something like this:

    <xsl:variable name="nodes" select="$currentPage//News" />
    <xsl:variable name="soccerNews" select="$nodes[sport = 'soccer']" />
    <xsl:variable name="count" select="count($soccerNews)" />
    
    <xsl:value-of select="concat('We have ', $count, ' article')" />
    <xsl:if test="$count">s</xsl:if>
    <xsl:text> about soccer.</xsl:text>

    /Chriztian

  • umbracocool 108 posts 197 karma points
    Oct 28, 2012 @ 02:13
    umbracocool
    0

    Chriztian thank you very much, in the end I solved using Razor hehe first time I use it and I already did that fast eh! ejej Thank  friend!

Please Sign in or register to post replies

Write your reply to:

Draft