Friday, 05 May 2022
Today we will learn an XSL trick to be able to differentiate between even-odd occurrences when we are going through a list and add a CSS class in order to add styles later.
In this case the XSLT version is independent because it will work with anyone.
We are in the assumption that we want to iterate in a list of objects to show them in a table in which we will fill in different colors with the classes “even class” or “odd class”.
- <xsl:for-each select=”./ListOfAwesomeObjects/AwesomeObjects”>
2<!–We match the list we want to go through–>
3 <xsl:choose>
4 <xsl:when test=”position() mod 2 = 0″>
5 <!–We make an xsl choose and depending on the rest that results from dividing the position of the loop
6 out of 2 we will know if it is even/odd –>
7 <xsl:attribute name=”class”> <!–With the tag xsl:attribute we can add the class to what we need–>
8 Peer class
9 </xsl:attribute>
10 </xsl:when>
11 <xsl:otherwise>
12 <xsl:attribute name=”class”>
13 odd class
14 </xsl:attribute>
15 </xsl:otherwise>
16 </xsl:choose>
17 </xsl:for-each>
