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).
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.
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).
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.
I found this online somewhere for a project a while back
Dn
Hi Andrew
In XSLT a loop is created with a for-each. Like this:
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
Thanks guys
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 <= $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
is working on a reply...
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.