Connecting Tech Pros Worldwide Help | Site Map

XSLT count items?

Newbie
 
Join Date: Dec 2008
Posts: 10
#1: Dec 28 '08
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?


Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="item"> 
  2.  
  3.      <div class="leftcolumn33">
  4.  
  5.  
  6. <!--Display Title and Description-->    
  7.     <h2><xsl:value-of select="title"/></h2>
  8.     <p><xsl:copy-of select="description/node()"/></p>
  9. <!--END Display-->                
  10.  
  11.  
  12.      </div> <!--End leftcolumn33 div-->
  13.  
  14.  
  15. <!--Test if the value in 'column' element is 3.  If so, add a 'cleardiv'-->
  16.      <xsl:if test="column ='3'"> 
  17.           <div class="cleardiv"/>
  18.      </xsl:if>
  19. <!--END Test-->
  20.  
  21.  
  22. </xsl:template>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Dec 28 '08

re: XSLT count items?


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
Newbie
 
Join Date: Dec 2008
Posts: 10
#3: Dec 28 '08

re: XSLT count items?


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


Expand|Select|Wrap|Line Numbers
  1. <item id="443476-MZF"> 
  2.      <column>1</column> 
  3.      <title>This is a title</title>
  4.      <description>This is a description</description>
  5. </item>
  6.  
  7. <item id="443477-MZF"> 
  8.      <column>2</column> 
  9.      <title>This is a title</title>
  10.      <description>This is a description</description>
  11. </item>
  12.  
  13. <item id="443478-MZF"> 
  14.      <column>3</column> 
  15.      <title>This is a title</title>
  16.      <description>This is a description</description>
  17. </item>
  18.  
  19. <item id="443479-MZF"> 
  20.      <column>1</column> 
  21.      <title>This is a title</title>
  22.      <description>This is a description</description>
  23. </item>
  24.  
  25. <item id="443480-MZF"> 
  26.      <column>2</column> 
  27.      <title>This is a title</title>
  28.      <description>This is a description</description>
  29. </item>
Newbie
 
Join Date: Dec 2008
Posts: 10
#4: Dec 28 '08

re: XSLT count items?


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:

Expand|Select|Wrap|Line Numbers
  1. <xsl:if test="position() mod 3 = 0">
  2. <div class="cleardiv"/>
  3. </xsl:if>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: Dec 28 '08

re: XSLT count items?


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


Similar XML bytes