Copied to clipboard

Flag this post as spam?

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


  • Andrew Blackmore 84 posts 127 karma points
    Mar 03, 2010 @ 21:39
    Andrew Blackmore
    0

    Emulating a loop in XSLT

    Hi All,

    I have a situation where it would be very helpful to use a loop but I don't really get how to in XSLT. I have this simple template and what I would like to do is have it continue looping until it finds no more blogImageList_images or hits a point (10 images or so).

    <xsl:template match="/">
        <xsl:if test="$currentPage/data [@alias='blogImageList_image1'] != ''">
          <a>
            <xsl:attribute name="href">
              <xsl:value-of select="umbraco.library:NiceUrl($currentPage/data [@alias = 'blogImageList_url1'])" disable-output-escaping="yes"/>
            </xsl:attribute>
            <img alt="{$currentPage/@nodeName}" class="blogImageList">
              <xsl:attribute name="src">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='blogImageList_image1'], 'false')/data [@alias = 'umbracoFile']" />
              </xsl:attribute>
            </img>
          </a>
          <br />
          <a>
            <xsl:attribute name="href">
              <xsl:value-of select="umbraco.library:NiceUrl($currentPage/data [@alias = 'blogImageList_url1'])" disable-output-escaping="yes"/>
            </xsl:attribute>
            <xsl:value-of select="$currentPage/data [@alias = 'blogImagesList_title1']"/>
          </a>
        </xsl:if>
    </xsl:template>

    I know that if it were in another language I would need to do something like

         for( i=0, 10, i !='' )

    {

    call template and i++;

    }

     

    I know XSLT doesn't work like that persay but I'm not truely understanding the best way to tackle this.

  • dandrayne 1138 posts 2262 karma points
    Mar 03, 2010 @ 21:44
    dandrayne
    0

    I found this online somewhere for a project a while back

    <xsl:template name="for.loop">

    <xsl:param name="i" />
    <xsl:param name="count" />
    <!--begin_: Line_by_Line_Output -->
    <xsl:if test="$i &lt;= $count">
    <option value="{$i}">
    <xsl:if test="$eventMinBreakoutRooms = $i">
    <xsl:attribute name="selected">selected</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="$i" />
    </option>
    </xsl:if>

    <!--begin_: RepeatTheLoopUntilFinished-->
    <xsl:if test="$i &lt;= $count">
    <xsl:call-template name="for.loop">
    <xsl:with-param name="i">
    <xsl:value-of select="$i + 1"/>
    </xsl:with-param>
    <xsl:with-param name="count">
    <xsl:value-of select="$count"/>
    </xsl:with-param>
    </xsl:call-template>
    </xsl:if>

    </xsl:template>


    <xsl:call-template name="for.loop">
    <xsl:with-param name="i">0</xsl:with-param>
    <xsl:with-param name="count">10</xsl:with-param>
    </xsl:call-template>

    Dn

  • Kim Andersen 1447 posts 2196 karma points MVP
    Mar 03, 2010 @ 21:48
    Kim Andersen
    0

    Hi Andrew

    In XSLT a loop is created with a for-each. Like this:

    <xsl:for-each select="$currentPage/node">
        Your code here...
    </xsl:for-each>

    Right now I don't know how your document type is build, so it's a little difficult to give you the excact code in your example. But the code above will loop for as many times, as the number of child-nodes that the current page has. So if the current page have got four children, the loop will loop four times. Hopes this makes sense to you.

    /Kim A

  • Andrew Blackmore 84 posts 127 karma points
    Mar 03, 2010 @ 21:55
    Andrew Blackmore
    0

    Thanks guys

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Mar 03, 2010 @ 22:29
    Chriztian Steinmeier
    1

    Hi Andrew,

    This is how I'd do something like what you're trying to accomplish:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:variable name="maxImageListImages" select="10" />
    
        <xsl:template match="/">
            <xsl:call-template name="outputImageListImage">
                <xsl:with-param name="node" select="$currentPage" />
            </xsl:call-template>
        </xsl:template>
    
    
        <xsl:template name="outputImageListImage">
            <xsl:param name="node" select="." /><!-- Makes it possible to call this from a for-each/apply-templates without even specifying the node -->
            <xsl:param name="index" select="1" /><!-- Assign default value if none was passed -->
    
            <xsl:variable name="mediaID" select="$node/data[@alias = concat('blogImageList_image', $index)]" />
            <xsl:variable name="linkID" select="$node/data[@alias = concat('blogImageList_url', $index)]" />
            <xsl:variable name="src" select="umbraco.library:GetMedia($mediaID, false())/data[@alias = 'umbracoFile']" />
    
            <xsl:if test="number($linkID) and normalize-space($src)">
                <xsl:variable name="url" select="umbraco.library:NiceUrl(linkID)" />
    
                <a href="{$url}">
                    <img class="blogImageList" alt="{$node/@nodeName}" src="{$src}" />
                </a>
                <br />
                <a href="{$url}">
                    <xsl:value-of select="$node/data[@alias = concat('blogImagesList_title', $index)]" />
                </a>
            </xsl:if>
    
            <xsl:if test="$index &lt;= $maxImageListImages">
                <!-- Call myself with the next index -->
                <xsl:call-template name="outputImageListImage">
                    <xsl:with-param name="index" select="$index + 1" />
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    
    </xsl:stylesheet>

    Feel free to ask about what's going on, if you like...  

    /Chriztian

  • 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