tags:

views:

39

answers:

2

I have the following nodes in xsl:

<foo>
    <bar>1</bar>
    <bar>2</bar>    
    <bar>3</bar>
    <bar>4</bar>        
    <bar>5</bar>
    <bar>6</bar>    
    <bar>7</bar>
    <bar>8</bar>            
    <bar>9</bar>
</foo>

And would like to turn it into the following html:

<ul class="one">
    <li>1</li>
    <li>4</li>
    <li>7</li>
</ul>
<ul class="two">
    <li>2</li>
    <li>5</li>
    <li>8</li>
</ul>
<ul class="three">
    <li>3</li>
    <li>6</li>
    <li>9</li>
</ul>

Having a hard time figuring out how to loop and get each third item, would like to do something like this:

<ul class="one">
<xsl:for-each select="exlt:node-set($blah)/foo/bar[X1]">
    <li><xsl:value-of select="node()"/></li>
</xsl:for-each>
</ul>

<ul class="two">
<xsl:for-each select="exlt:node-set($blah)/foo/bar[X2]">
    <li><xsl:value-of select="node()"/></li>
</xsl:for-each>
</ul>

<ul class="three">
<xsl:for-each select="exlt:node-set($blah)/foo/bar[X3]">
    <li><xsl:value-of select="node()"/></li>
</xsl:for-each>
</ul>   

Where:
X1 = Every third item starting from position 1
X2 = Every third item starting from position 2
X3 = Every third item starting from position 3

Might need to use last(), but can't quite get that working correctly.

+1  A: 

In XPath, the condition will be:

not(position() mod 3)

or

position() mod 3 = 0

I don't see why you can't use op:mod.

EDIT: About new question, just substract the offset. So:

X1:

position() mod 3 = 1

X2:

position() mod 3 = 2

X3:

position() mod 3 = 0

EDIT 2: Now I understand your question.

Alejandro
Thought I could use mod too, but how would you loop through the other columns. Updated the question with more details. Maybe I am just missing something totally obvious. Look at column 2 it should be #s: 2,5,8 these are not equal mod.
Louis W
Thanks! I guess I totally misunderstood what mod did, thought it was only 1 or 0 depending on if the number was divisible. Only ever used it for alternating rows.
Louis W
@Louis W: You are wellcome! From http://www.w3.org/TR/xpath/#numbers : "The mod operator returns the remainder from a truncating division".
Alejandro
A: 

Try [position() mod 3 = 1]

schoetbi
It should be 0 of course:-)
schoetbi