Copied to clipboard

Flag this post as spam?

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


  • Rao 10 posts 30 karma points
    Aug 11, 2011 @ 18:15
    Rao
    0

    foreach loop in XSLT

    Hello All,

    Thanks in advance.

    I need to display the subnodes in the container page.

    Mani page/node: Node1

    Subnodes:

     

    1. SubNode1
    2. SubNode2
    3. SubNode3
    4. Subnode4
    5. Subnode5
    each subnode will have some properties, those properties needs to be shown on the page.
    I wanted to display these subnodes in a page like:
    first row:
    Subnode1           subnode2        subnode3
    .name                  .name             .name
    .value                   .value             .value
    second row:
    Subnode4           subnode5
    .name                  .name            
    .value                   .value             
    I am able to display the nodes as required using the below foreach:
    <xsl:template match="/">    
      start
       <xsl:for-each select="$currentPage/* [@isDoc][position() mod 3 = 1]">
          first loop 
          <div>
            <xsl:for-each select=". | following-sibling::*[not(position() >= 2)]">
             second loop
            </xsl:for-each>
          </div>
      </xsl:for-each>
    </xsl:template>
    But the 1st and 2nd are having the same nodeId(1st node id), 4th & 5th are having same nodeid(4th nodeId).
    3rd is having the nodeId of 2nd.
    Can you please help me in this regard.

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 11, 2011 @ 20:52
    Fuji Kusaka
    0

    Hi Rao,

    I think you can get this working by trying this. Assuming the first row will always consist of only 3 content nodes.

     

    <xsl:template match="/">
      <xsl:variable name="nodesToShow" select="3"/>

    <!-- The fun starts here -->

      
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']"
      
      <xsl:choose>    
        <xsl:when test="position()&lt;= $nodesToShow">         
                First Row
                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a> 
            
        </xsl:when>
        
    <xsl:otherwise>
      Row 2
           
                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                </a>
                
           
    </xsl:otherwise>
    </xsl:choose>
      
    </xsl:for-each>
  • Rao 10 posts 30 karma points
    Aug 11, 2011 @ 21:57
    Rao
    0

    Thanks for your response Fuji.

    When I tried this, I am getting the result like 

    Second - Bias - Freedom - Sound - TS
    But I want results like below:
    Sound - TS
  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 11, 2011 @ 22:04
    Fuji Kusaka
    0

    How are you displaying your content in a Div or Table Row?

     

    //fuji

  • Rao 10 posts 30 karma points
    Aug 11, 2011 @ 22:10
    Rao
    0

    I tried both the ways...Thanks.

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 11, 2011 @ 22:39
    Fuji Kusaka
    0

    I prefer Div since i have more controls for my part....you could try something like that

    <div style="float:left; width:600px;">
     
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']"> 
      
      <xsl:choose>       
          <xsl:when test="position()<= $nodesToShow">         
                <div style="float:left; width:190px; padding:0 5px; display:block;">
                <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                a
           div >         
        xsl:when
        <xsl:otherwise>
            
               <div style="float:left; width:190px; padding:0 5px; display:block;">
                   <a href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                a>
                div >                    
        xsl:otherwise>    
      xsl:choose>
    xsl:for-each>    
       
      div>


    All the Child Div will float to the left and will thus automatically  go to the next row.
    you can also use Ul List but basically its the same  principal as div layout.

     

    Regards


    //fuji

  • Rao 10 posts 30 karma points
    Aug 12, 2011 @ 14:54
    Rao
    0

    Thanks a lot Fuji. It worked.

    But if user adds 3 more nodes, it's displaying the results like below:

    Second - Bias - Freedom 
    Sound - TS - One
                            Two
    Three
  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 13, 2011 @ 19:19
    Fuji Kusaka
    0

    Hey Rao,

    Good to hear its working. Yes you might need to make some modifications to the Stylesheet for the other Content Row to display the values inline.

    Am not sure if this can help but you can give this a try assuming only the first 3 nodes will always be displayed on the first row.

     <xsl:variable name="nodesToShow" select="3"/>
      <xsl:variable name="changeRow" select="4"/>



    <!-- The fun starts here -->
     <table border="1" cellpadding="0" cellspacing="0" width="800">
     <tr>
       <td>
         <xsl:for-each select="$currentPage/* [@isDoc and  string(umbracoNaviHide) != '1']" >
             <xsl:if test="position()&lt;= $nodesToShow">                  
                         
                             <div style="float:left; width:auto; padding:0 5px; display:block;">
                <href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                            </a>
               </div>
                                            
              </xsl:if
         </xsl:for-each>
       
       </td>
       </tr>
       <tr>
         <td>
            <xsl:for-each select="$currentPage/* [@isDoc and  string(umbracoNaviHide) != '1']" >
             <xsl:if test="position()&gt;= $changeRow">                  
                         
                          <div style="float:left; width:auto; padding:0 5px; display:block;">
                <href="{umbraco.library:NiceUrl(@id)}">
                  <xsl:value-of select="@nodeName"/>
                            </a>
               </div>
              
                                            
              </xsl:if
         </xsl:for-each>
         </td>
       </tr>
      </table>

     

    //fuji

  • Rao 10 posts 30 karma points
    Aug 18, 2011 @ 20:42
    Rao
    0

    Thanks Fuji for all your help.

    The example you have provided me did not worked as expected.

    I have made some tweaks to my for-each loop based on what you have provided, now it's working as expected

    <xsl:template match="/">    
      start
       <xsl:for-each select="$currentPage/* [@isDoc][position() mod 3 = 1]">
          first loop 
          <div>
            <xsl:for-each select=". | following-sibling::*[not(position() >= 2)]">
             second loop
            </xsl:for-each>
          </div>
      </xsl:for-each>
    </xsl:template>

     

    Again thanks for all your help in resolving this.

     

Please Sign in or register to post replies

Write your reply to:

Draft