Copied to clipboard

Flag this post as spam?

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


  • Paul Hugo 3 posts 23 karma points
    Oct 01, 2012 @ 17:57
    Paul Hugo
    0

    For-each and string concatenating

    Hi All,
    I would very much appreciate dsome help.  I'm new to Umbraco, xslt and all things .net and I have hit a stumbling block.  I have a for-each loop and I nned to add an asterisk to a variable during each loop. I have the following but the variable "bullet" never changes, it is always a single asterisk.  What am I doing wrong?

    <?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="bullet" select="'*'" />
        
    <xsl:template match="/">
        <xsl:for-each select="umbraco.library:GetXmlNodeByXPath('.')/descendant-or-self::umbHomepage[@nodeName='Homepage Banners']/HomepageBanner">
          <a href="#"><xsl:value-of select='$bullet' />Click here to expand and view full terms and conditions for the <xsl:value-of select='description' /></a><br/>');
          <xsl:variable name="bullet" select="concat($bullet,'*')" />
        </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

     

    Any help would be greatly appreciated

    Thanks

    Paul

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 01, 2012 @ 18:55
    Chriztian Steinmeier
    0

    Hi Paul,

    Welcome to the forums!

    As you will probably soon learn, XSLT demands a totally different approach than your average "normal" programming language (it's actually a lot more like CSS).

    I'm just going to post a "solution" to what you're trying to do, and then you can ask anything you want about how it works, but try to wrap your head around as much as you can first, before shooting, OK?

    <xsl:param name="currentPage" />
    
    <!-- This can be optimized depending on Content structure -->
    <xsl:variable name="bannerRoot" select="$currentPage/ancestor-or-self::root//umbHomepage[@nodeName = 'Homepage Banners']" />
    
    <xsl:variable name="bullets" select="'**************************************************'" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="$bannerRoot" />
    </xsl:template>
    
    <xsl:template match="HomepageBanner">
        <a href="#">
            <xsl:value-of select="substring($bullets, 1, position())" />
            <xsl:value-of select="concat('Click here to expand and view full terms and conditions for the ', description)" />
        </a>
    </xsl:template>
    

    /Chriztian

  • Paul Hugo 3 posts 23 karma points
    Oct 02, 2012 @ 12:38
    Paul Hugo
    0

    Ah, thanks Chriztian.  Makes perfect sense.  The only question I have is; is ther no way to dynamically assign the number of asterisks in the bullets variable based on the number of Homepage Banner nodes?  It is not likely to happen but if there were more banners than asterisks, the script would not function as it should.

    Thanks again for your speedy reply.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 02, 2012 @ 13:07
    Chriztian Steinmeier
    0

    Hi Paul,

    I thought that would come up :-)

    Here's how you can do that:

    <xsl:variable name="bullets">
        <xsl:for-each select="$bannerRoot/HomepageBanner"><xsl:text>*</xsl:text></xsl:for-each>
    </xsl:variable>

     

    /Chriztian

  • Paul Hugo 3 posts 23 karma points
    Oct 02, 2012 @ 13:13
    Paul Hugo
    0

    Perfect.  I'm used to php so this is a completely new way of thinking!  Thanks so much for your help!

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 02, 2012 @ 15:57
    Dan Diplo
    1

    Paul, you can always use "Razor" macros rather than XSLT, which will be a bit more familiar to you if you are a PHP developer. See http://our.umbraco.org/documentation/v480/Reference/Querying/DynamicNode/

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies