Trouble using XSLT variables to hold attributes

variables, xslt

Solution

After reading in the meanwhile I found out(something that however I knew, mostly) that:

The fact that you need to modify an shows that you are not thinking in XSLT.

XSLT is a functional programming language, which, among other things means that there is no way one can change a variable. Variables in a functional programming language by definition are immutable. Source: http://social.msdn.microsoft.com/Forums/eu/xmlandnetfx/thread/2bf7d238-92d2-4453-a683-4bfb0c667795

So I have changed my logic a little: I put all conditions within xsl attribute like this:

 <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; '0'">
            <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
                <li>
                    <xsl:attribute name="class">
                        page topNavigLi page<xsl:number value="position()" format="1" />
                        <xsl:if test="@id = $currentPage/@id">
                            current 
                        </xsl:if>
                        <xsl:if test="position() = last()">
                            last
                        </xsl:if>
                        <xsl:if test="@nodeName='Network' ">
                            has_submenu network
                        </xsl:if>
                    </xsl:attribute>

So no need of extra temp var.

Problem

In my xslt template I am having a `for-each` statement. In that `for-each` I am making different conditions. I want to have there a variable of string type which shall contain class attributes that will be assigned to a `<li>`. As I am new to xslt please provide me some examples or how can I achieve what I want to do. Here is a bit of my code so you can see what I am taking about: ``` <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; '0'"> <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']"> <li><xsl:attribute name="class"> topNavigLi page<xsl:number value="position()" format="1" /> <xsl:if test="@nodeName='Network' "> has_submenu network </xsl:if> </xsl:attribute> <xsl:if test="@id = $currentPage/@id"> <xsl:attribute name="class"> current topNavigLi page<xsl:number value="position()" format="1" /> </xsl:attribute> </xsl:if> <xsl:if test="position() = last()"> <xsl:attribute name="class"> last topNavigLi page<xsl:number value="position()" format="1" /> </xsl:attribute> </xsl:if> <xsl:if test="@id = $currentPage/@id and position() = last()"> <xsl:attribute name="class"> current last topNavigLi page<xsl:number value="position()" format="1" /> </xsl:attribute> </xsl:if> ``` It would be much elegant to have a variable and than concatenate to it while doing tests. I have tried like this but returns nothing. ``` <xsl:variable name="li_class" select="page"> </xsl:variable> <xsl:attribute name="class"> <xsl:value-of select="li_class" /> </xsl:attribute> ```

Original source