Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Apr 07, 2011 @ 12:34
    Tom
    0

    Reassign Variable

    Hi Guys,

    I'm trying to reassign a variable in xslt or attempt to do something similar.. I'm trying to loop through a series of product categories.. if the current product category is the one in the loop I want to assign it's position to a variable.. if there is no current category I'd like to assign the index to 0

    What im trying to do is basically in jQuery tools.. if a category exists pass its index into the javascript so it sets that tab visible on page load..

     

     <xsl:for-each select="$allProductCategories">
        
        <xsl:variable name="currentCategoryIndex">
        <xsl:if test="$currentProductCategoryID = @id">
          <xsl:value-of select="position()"/>
        </xsl:if>
        </xsl:variable>

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 07, 2011 @ 12:48
    Lee Kelleher
    0

    Hi Tom,

    While you can't reassign a variable explicitly (as in they are "variable by type, not value"), from your explanation, you should be able to do what you want here.

    Variables in XSLT are defined by scope, so since you are within a for-each loop, the variable only has scope for that iteration - hence why you can "re-assign" it... like so:

    <xsl:for-each select="$allProductCategories">
        <xsl:variable name="currentCategoryIndex">
            <xsl:choose>
                <xsl:when test="$currentProductCategoryID = @id">
                    <xsl:value-of select="position()"/>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
    
        ...
    
    </xsl:for-each>

    Cheers, Lee.

  • Tom 713 posts 954 karma points
    Apr 08, 2011 @ 01:40
    Tom
    0

    Hi Lee thanks for your reply... the biggest problem is I need to use the variable outside of the for-each loop..

    I ended up getting around it by assigning a var in javascript and then using that javascript var outside my loop in a javascript function call... which worked for this scenario.. id be keen to know how you'd make it work using straight xslt..

Please Sign in or register to post replies

Write your reply to:

Draft