XSLT count items?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shifflav
    New Member
    • Dec 2008
    • 10

    #1

    XSLT count items?

    The code below puts each item in a float:left DIV. After each 3rd DIV it needs to clear the previous 3 DIVs and start a new row. The code below works, but it requires that I edit a 'column' element in my XML file. Every 3rd item in my XML file has a <column>3</column> element added to it so the xsl:if test code knows when to add my clearing DIV.

    This seems inefficient to me and I'm sure there is a way the XSLT processor can insert my <div class="cleardiv "/> after each 3rd item. Any ideas?


    Code:
    <xsl:template match="item"> 
    
         <div class="leftcolumn33">
       
    
    <!--Display Title and Description-->	
    	<h2><xsl:value-of select="title"/></h2>
    	<p><xsl:copy-of select="description/node()"/></p>
    <!--END Display-->				
    
    
         </div> <!--End leftcolumn33 div-->
    
    
    <!--Test if the value in 'column' element is 3.  If so, add a 'cleardiv'-->
         <xsl:if test="column ='3'"> 
              <div class="cleardiv"/>
         </xsl:if>
    <!--END Test-->
    
    
    </xsl:template>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    a quick idea (without testing)......

    if position() % 3 = 0

    try the modulo operator on the position number (if the description elements are siblings), otherwise I'd need a look at the XML

    sorry for not having much time

    Comment

    • shifflav
      New Member
      • Dec 2008
      • 10

      #3
      XML code

      OK...here is the XML code as requested.


      Code:
      <item id="443476-MZF"> 
           <column>1</column> 
           <title>This is a title</title>
           <description>This is a description</description>
      </item>
      
      <item id="443477-MZF"> 
           <column>2</column> 
           <title>This is a title</title>
           <description>This is a description</description>
      </item>
      
      <item id="443478-MZF"> 
           <column>3</column> 
           <title>This is a title</title>
           <description>This is a description</description>
      </item>
      
      <item id="443479-MZF"> 
           <column>1</column> 
           <title>This is a title</title>
           <description>This is a description</description>
      </item>
      
      <item id="443480-MZF"> 
           <column>2</column> 
           <title>This is a title</title>
           <description>This is a description</description>
      </item>

      Comment

      • shifflav
        New Member
        • Dec 2008
        • 10

        #4
        Got it!

        Thanks for the push in the right direction. I found that it would not accept the mod operator and I had to actually use the word 'mod' in my code. Thanks again...

        Here is what worked:

        Code:
        <xsl:if test="position() mod 3 = 0">
        <div class="cleardiv"/>
        </xsl:if>

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          I'm glad that such a little push yielded such a success. ;-)

          Comment

          Working...