Solved

How to I "stripe" columns using XSLT

Posted on 2014-01-13
3
38 Views
Last Modified: 2016-05-18
Given an XML "table" with an arbitrary and unknown number of columns, what is the best way to handle column even/odd formatting so that the results are striped columns?
0
Comment
Question by:heykjo
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39777470
CSS
	tr td:nth-child(odd){ 
		background: blue;
	}
	tr td:nth-child(even){
		background: green;
	}	

Open in new window


This is CSS3 so won't work in IE8 and lower.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39777515
well, given the question clearly says "using XSLT", I can offer you a XSLT solution that works regardless of the browser version

the trick is to use "mod" on position of cells inside the row (I assume your XML model is row based not column based, if it is column based, you need to pivot in order to get html anyway)

on the cell you do this (assuming an apply templates for the cells only, and assuming cell as the cell element name in your XML)
<xsl:template match="cell">
<td>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">cell-odd</xsl:when>
<xsl:otherwise>cell-even</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."/>
</td>
</xsl:template>

so you can have in your css a style for cell-even and cell-odd, setting the background color
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML to SQL Table using c# 5 56
c# code 19 69
Fixing Non-Standard characters in text 8 92
What language/protocol is the Angular Chat? 2 57
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now