Copied to clipboard

Flag this post as spam?

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


  • Fredrik 89 posts 108 karma points
    Oct 26, 2010 @ 09:55
    Fredrik
    0

    How do I solve this if I can't break a for-each loop

    <xsl:for-each select="$currentPage/descendant::* [@isDoc and string(umbracoNaviHide) != '1']">
     <xsl:if test="highlight = 1">

      <xsl:variable name="mediaId" select="number(./image)" /> 

      <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
      <xsl:value-of select="$mediaNode/umbracoFile"/>

    </xsl:if>
      

     </xsl:for-each>

     

    I want this loop to just include the first hit on highlight = 1

    If it hits two times the output may look like this:

    /media/68/ingen_bild.jpg/media/171/a113_tvattmedel.jpg

    which is useless as an image source. I know I cant break a for-each in xslt...so how should I approach this problem?

    /Fredrik

     

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 26, 2010 @ 09:59
    Kim Andersen
    0

    Hi Fredrik

    You should be able to use the position() in xslt to solve your problem like this:

    <xsl:if test="position()='1'">

    You can place it outside the other if-statement you've got, or probably just merge the two of them together with an 'and' :)

    /Kim A

  • Fredrik 89 posts 108 karma points
    Oct 26, 2010 @ 10:19
    Fredrik
    0

    The problem with position is that it depends on the node that is first hit byt the if statement.

    Therefore position()='1' wont work...it can be anything 4, 12, 3.

    What do you mean by merging them together with an and, how would that solve the problem?

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 26, 2010 @ 10:24
    Kim Andersen
    1

    Ahh yeah, of course, didn't think of that. Maybe you should change the for-each as well:

    <xsl:for-each select="$currentPage/descendant::* [@isDoc and string(umbracoNaviHide) != '1' and string(hightlight)='1']">

    Then the for-each should only run through the nodes which has the highlight property set to 1. And then you can use the position()=1 inside the for-each.

    Does that work?

    /Kim A

  • Fredrik 89 posts 108 karma points
    Oct 26, 2010 @ 10:33
    Fredrik
    0

    Works like a charm!

  • 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