Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Feb 04, 2014 @ 10:07
    Craig O'Mahony
    0

    Set the value of a variable in for-each loop and get value outside of loop

    Hi all,

    Am stuck on something I'm trying to set the value of a variable inside a for-each loop and then use the variables value outside of the loop:

    <xsl:for-each select="$homeNode/* [@isDoc and @level = 3 ]">
    <xsl:if test="@nodeName = $teamName">
    <xsl:variable name="nodeCount" select="ceiling(count(./child::*[@isDoc and string(umbracoNaviHide) != '1'])div 2)" /> ******THIS IS WHERE I'M SETTING THE VARIABLE
    <!--Get the skills from the children-->
    <xsl:for-each select="./child::*[@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:value-of select="@nodeName" />
    </xsl:for-each>
    </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="$nodeCount"/> *****This is the variable I'm trying to get

    But the code keeps erroring saying that the variable is outside of scope (which it is) but I don't know how to decalre and set it.

    Could anybody help please?

    Thanks,

    Craig

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 04, 2014 @ 10:29
    Chriztian Steinmeier
    100

    Hi Craig,

    Variables in XSLT are only for stuff that changes between transformations, so they can't be changed, and are therefore declared and assigned in one operation.

    Applying C# (or similar "standard" programming) concepts to XSLT doesn't really work that well - here's an XSLT version of what I think you're trying to do:

    <!-- Grab the teamnode right away -->
    <xsl:variable name="teamNode" select="$homeNode/*[@isDoc][@nodeName = $teamName]" />
    
    <!-- Grab the childnodes to display -->
    <xsl:variable name="childNodes" select="$teamNode/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
    <!-- Get the "count" -->
    <xsl:variable name="nodeCount" select="ceiling(count($childNodes) div 2)" />
    
    <!-- Get the skills from the team's childnodes -->
    <xsl:for-each select="$childNodes">
        <xsl:value-of select="@nodeName" />
    </xsl:for-each>
    
    <!-- Output the "count" -->
    <xsl:value-of select="$nodeCount" />
    

    /Chriztian

  • Craig O'Mahony 364 posts 918 karma points
    Feb 04, 2014 @ 10:39
    Craig O'Mahony
    0

    Perfect! Much more elegant!

    Chriztian you a star and I shall bear this in mind for future.

    Thanks,

    Craig

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Feb 04, 2014 @ 18:01
    Fuji Kusaka
    0

    Hi Craig,

    One easy way of doing this is by making use of template.

  • Fuji Kusaka 2203 posts 4220 karma points
    Feb 04, 2014 @ 18:04
    Fuji Kusaka
    0

    Sorry Guys, just notice Chriztian replied.

     

Please Sign in or register to post replies

Write your reply to:

Draft