Copied to clipboard

Flag this post as spam?

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


  • Mark W. Nielsen 13 posts 33 karma points
    Aug 05, 2011 @ 11:17
    Mark W. Nielsen
    0

    Change value of XSLT Variable inside a for-each

    Hello Umbraco Community.

    I'm currently banging my head against a wall. My problem is that i'm trying to change the value of a <xsl:variable name="news_background_color">green</xsl:variable> inside of my <xsl:for-each....

    MY full XSLT code is right here:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt" 
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <xsl:variable name="news_background_color">green</xsl:variable> <---- WANT TO CHANGE THIS ONE

    <xsl:template match="/">

    <!-- The fun starts here -->
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
      <xsl:sort select="@createDate" order="descending"/>
      
      <div id="news_list_{$news_background_color}">
        <class="news_date"><xsl:value-of select="umbraco.library:LongDate(@createDate)"/></p>
          <h2><xsl:value-of select="@nodeName"/></h2>
        <class="news_teaser">
          <xsl:value-of select="newsSummary"/>
        </p>
        <class="read_more"><class="read_more" href="{umbraco.library:NiceUrl(@id)}">Read more...</a></p>
      </div>
      <xsl:choose>                                                        <----from here---|
        <xsl:when test="$news_background_color='green'">        |
          <xsl:variable name="news_background_color">white</xsl:variable>                  |
        </xsl:when>        |
        <xsl:otherwise>        |
          <xsl:variable name="news_background_color">green</xsl:variable>                  |
        </xsl:otherwise>        |
      </xsl:choose>       <-----to here----|

    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>

     

    But i'm completely lost at the moment. So hoping that some of you guys can help me.

    Regards,

    Mark W. Nielsen.

  • Mark W. Nielsen 13 posts 33 karma points
    Aug 05, 2011 @ 11:59
    Mark W. Nielsen
    0

    I solved the problem after some searching for alternative (but easier) methods.

    I changed the ideer about using a variable, and instead i choosed to use simple mats. You can see the new XSLT code right here:

    <xsl:param name="currentPage"/>
    <xsl:variable name="news_position" select="position()"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
      <xsl:sort select="@createDate" order="descending"/>
      <xsl:choose>
        <xsl:when test="position() mod 2 = 1">
          <div id="news_list_green">
            <class="news_date"><xsl:value-of select="umbraco.library:LongDate(@createDate)"/></p>
              <h2><xsl:value-of select="@nodeName"/></h2>
            <class="news_teaser">
              <xsl:value-of select="newsSummary"/>
            </p>
            <class="read_more"><class="read_more" href="{umbraco.library:NiceUrl(@id)}">Read more...</a></p>
          </div>   
        </xsl:when>
        <xsl:otherwise>
          <div id="news_list_white">
            <class="news_date"><xsl:value-of select="umbraco.library:LongDate(@createDate)"/></p>
              <h2><xsl:value-of select="@nodeName"/></h2>
            <class="news_teaser">
              <xsl:value-of select="newsSummary"/>
            </p>
            <class="read_more"><class="read_more" href="{umbraco.library:NiceUrl(@id)}">Read more...</a></p>
          </div>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>

     

    So what i did is to use the position instead, and that combined with some simple math make it all work like it should.

    Regards,

    Mark W. Nielsen

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 05, 2011 @ 12:03
    Fuji Kusaka
    0

     

    Hi Mark,

     

     Am nto sure waht you are trying to achive her. Do you want to alternate the background color of your DIV in the loop?

     

    //fuji

     

  • Mark W. Nielsen 13 posts 33 karma points
    Aug 05, 2011 @ 12:09
    Mark W. Nielsen
    0

    That was excatly what i was trying to... :)

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 05, 2011 @ 12:34
    Fuji Kusaka
    0

    Great you got it working.:)

     

    //fuji

Please Sign in or register to post replies

Write your reply to:

Draft