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.
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" />
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:
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
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:
/Chriztian
Perfect! Much more elegant!
Chriztian you a star and I shall bear this in mind for future.
Thanks,
Craig
Hi Craig,
One easy way of doing this is by making use of template.
Sorry Guys, just notice Chriztian replied.
is working on a reply...