I want to alternate background color in a for-each. So the first value-of will be blue, than green, than blue again... But I can't use position() because sometime some values are not displayed (it's a docType parameter). So, I would have two blue or green value in a row.
Are you perhaps using an if statement inside the loop to filter out the items that ypu don't want to be displayed?
If so, you could also filter these out in the for-each select (e.g. with a guard [@docType = 'thisOne']) so you are sure you are only looping through items that need to be displayed, and can still use position().
<xsl:for-each select="$records"> <!-- Look for Yes to publish and empty parentID --> <xsl:if test="./fields/autoriserlapublication//value = 'Yes' and string(./fields/parentid//value) = ''"> <cite style="margin-left:3px;"><xsl:value-of select="./fields/commentaire//value"/></cite> <!-- Now, for each records that have a value in parentID --> <xsl:variable name="parentID" select="id"/> <xsl:for-each select="$records[string(./fields/parentid//value) = $parentID]"> <xsl:if test="./fields/autoriserlapublication//value = 'Yes'"> <cite> <xsl:value-of select="./fields/commentaire//value"/></cite> </xsl:if> </xsl:for-each> </xsl:if> </xsl:for-each>
So, I want to change background color for the first for-each. But sometimes position is odd (or even) for two or more "main" records in a row. I try to make answers to comment. So in the first loop it displays main comments and in the inner loop it displays answer to the main comment.
how to alternate background color
Hi guys!
I want to alternate background color in a for-each. So the first value-of will be blue, than green, than blue again... But I can't use position() because sometime some values are not displayed (it's a docType parameter). So, I would have two blue or green value in a row.
Do you have alternative to position() ?
Thank you.
Are you perhaps using an if statement inside the loop to filter out the items that ypu don't want to be displayed?
If so, you could also filter these out in the for-each select (e.g. with a guard [@docType = 'thisOne']) so you are sure you are only looping through items that need to be displayed, and can still use position().
If it's help :
So, I want to change background color for the first for-each. But sometimes position is odd (or even) for two or more "main" records in a row. I try to make answers to comment. So in the first loop it displays main comments and in the inner loop it displays answer to the main comment.
So I don't think I can use you idea.
Hi,
I will try to help you as best as I can.
But after reading your quistion, I thought about how you can solve it like this.
The way I think it can be resolved is through the modulus.
So in your for-each statement you make a choose construction.
I hope you find a solution.
If this is the correct way to do it, I hope to bear with me :)
/Dennis
I think you should still be able to use position() in your main loop if you drop the if statement, like so:
<xsl:for-each select="$records[./fields/autoriserlapublication//value = 'Yes' and string(./fields/parentid//value) = '']">
<cite style="margin-left:3px;"><xsl:value-of select="./fields/commentaire//value"/></cite>
<xsl:variable name="parentID" select="id"/>
<xsl:for-each select="$records[string(./fields/parentid//value) = $parentID]">
<xsl:if test="./fields/autoriserlapublication//value = 'Yes'">
<cite> <xsl:value-of select="./fields/commentaire//value"/></cite>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
Yeah you were right! I can use position() if I drop the if statement!
is working on a reply...