469,282 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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?


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>
Dec 28 '08 #1
4 4378
Dormilich
8,651 Expert Mod 8TB
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
Dec 28 '08 #2
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>
Dec 28 '08 #3
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>
Dec 28 '08 #4
Dormilich
8,651 Expert Mod 8TB
I'm glad that such a little push yielded such a success. ;-)
Dec 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by inquirydog | last post: by
6 posts views Thread by Per Jørgen Vigdal | last post: by
11 posts views Thread by ned786 | last post: by
1 post views Thread by jrwarwick | last post: by
4 posts views Thread by J | last post: by
4 posts views Thread by bobiron | last post: by
2 posts views Thread by Adam dR. | last post: by
10 posts views Thread by olivier.scalbert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.