Copied to clipboard

Flag this post as spam?

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


  • Mike 80 posts 101 karma points
    May 10, 2011 @ 16:09
    Mike
    0

    Need some help with a loop in xslt

    I need some help because I've been staring at the screen and searching the internet for too long. Maybe I'm just thinking too difficult...

    I have a setup like this

    node1
    |_node2
      |_sub1
      |_sub2
      |_etc.

    I need to loop through the children of node2 so I get the sub nodes. I need them in "chunks" of 5 subnodes.

    The output needs to be something like this: 

       <!-- root element for the items -->
       <div class="items">
    
          <!-- 1-5 -->
          <div>
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
          </div>
    
          <!-- 5-10 -->
          <div>
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
          </div>
    
          <!-- 10-15 -->
          <div>
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
             <img src="" />
          </div>
    
       </div>

    I tried a for-each and check position() mod 5 to insert a <div> but that got me nowhere. I'm kind off XLS blinded right now so i hope somebody has a solution for this. probably easy to do... i hope ...

    thanks

     

  • Sean Holmesby 61 posts 82 karma points
    May 11, 2011 @ 02:07
    Sean Holmesby
    0

    This was a fun one to work on!!! I had no idea where to start with this... but I found the answer...

    You want to loop over the nodes that require the <div> first. This will be 1, 6, 11, 16.... so position() mod 5 = 1 will get you those indexes.

    Then within that loop, you want to go from that node, to the next 5 nodes. Do this using the 'following-sibling' feature in XSLT.

    Your code will look like this:-

     

    <xsl:for-each select="./item[position() mod 5 = 1]">

    <div>

    <xsl:for-each select=". | following-sibling::*[not(position() >= 5)]">

    <xsl:value-of select="@nodeName" /><br />

    </xsl:for-each>

    </div>

    </xsl:for:each>

     

    Enjoy.

    - Sean

  • Mike 80 posts 101 karma points
    May 11, 2011 @ 09:40
    Mike
    0

    haha, i'm glad somebody had fun with this ;-) 

    thanks! i'm gonna try it right away...

     

  • Mike 80 posts 101 karma points
    May 11, 2011 @ 10:12
    Mike
    0

    hmm.. should this code work "out of the box", or does it need some modifcations?

    when i put this in a macro on "node2" (or "node1", but should be inserted at node2), I don't get any results...

    here's what i did:

    <xsl:template match="/">    
      start
        <xsl:for-each select="./item[position() mod 5 = 1]">
          
          in loop 
        
          <div>
            <xsl:for-each select=". | following-sibling::*[not(position() >= 5)]">
              in second loop
              <xsl:value-of select="./@nodeName" /><br />
            xsl:for-each>
          div>
      xsl:for-each>
      
      
    xsl:template>

    on the site it will only output "start".

    what does item[] do? is this the same as for example: $currentPage/ancestor-or-self [position()=2] ?

    Why don't you use $currentPage here: xsl:for-each select="./item[position() mod 5 = 1]">  or is a "." the same as currentPage?


     

     

  • Mike 80 posts 101 karma points
    May 11, 2011 @ 10:58
    Mike
    0

    I change the first for-each to:

     

    <xsl:for-each select="$currentPage/* [@isDoc][position() mod 5 = 1]">

    Which seems to work! 

    Still not sure what the second for-each loop reads in pseudo.. for example:

    for each currentnode OR (? not sure pipe symbol) the node next to it as long as the position < 5 do

    because i'd like to understand what the code does...

    thanks

     

     

     

  • Sean Holmesby 61 posts 82 karma points
    May 12, 2011 @ 03:22
    Sean Holmesby
    0

    The $currentPage works there because that's the page that you are currently on. My code was intended to be in it's own template, which you call using $currentPage, so it's essentially the same thing. I'm not sure of your knowledge of XSLT, so I can explain this further if you like?

    The second for-each loop works pretty much the way your pseudo code says. The pipe acts as an OR, and we go through the nodes, starting with the current one that you're 'talking about' (i.e the item from the first loop (items 1, 6, 11, 16...)), and choosing each node along from that, as long as it's not more than 5 places along from it.

    It's a little confusing, even for me, but your pseudo code seems to show that you get the gist of it.

     - Sean

Please Sign in or register to post replies

Write your reply to:

Draft