Skip to content
Autana Business Partners

Snippets XSL- Select even-odd occurrences.

Published on 1 min read

autanabp

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".

  1. <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>

Updated on

Tags

← Back to the blog

  • Data Management

    Optimization of energy consumption

    Discover how the optimization of energy consumption reduces costs and environmental impact. Learn to analyze data, simulate scenarios, and implement energy-saving strategies.

    Read article

  • Analytics

    Talent retention in the digital age: how analytics can help

    Learn how talent retention in the digital age benefits from analytics. Discover how data helps reduce turnover and retain your top employees.

    Read article

  • Success Stories

    Proyect Sourcing Optimization

    In the digital age, Project Sourcing Optimization has become essential for companies aiming to enhance their purchasing processes. By integrating data-driven decision-making and…

    Read article