Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS formatting based on HTML attributes 2

Status
Not open for further replies.

AndoSam

Technical User
Jul 14, 2010
16
GB
Hi

I am relatively new to (X)HTML, CSS etc. and have encountered a slight issue with a task I am currently working on.

I am using a Perl script to write an xml file which then is formatted by an xsl document with a css stylesheet (This bit all works fine).

A table is created by my code which I would like to have alternating row colours. I have succesfully applied this to most of the table by creating classes using

Code:
<td class="r{position() mod 2}">
and the relative css
Code:
.r0 { background-color : #E0E0E0 }
.r1 { background-color : #D0D0D0 }

However, there is one particular entry that needs a different class name for some other formatting.

Is it possible to apply css formatting depending on a classes attributes rather than a class name?

Thanks in advance
 
Code:
<td class="r{position() mod 2}">
What markup is that?

Anyway, you can't have two class names the same with different attributes, you define a class with its attributes, that class name will always have the CSS attrbiutes you define.

You can dynamically alter CSS calss attributes, but that will still affect ALL elements that have that class applied to it.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
It is some syntax I found on a forum/post somewhere. It basically just alternates rows between classes r0 and r1 by taking the modulus of two of its row position (i.e. always 0 or 1).

I understand that a single class name can't have different attributes, however I was considering using an xsl:if implementation to change the value of attributes.
The contents of r0 and r1 can be one of 4 values and I want the font color to change for each one, but also retain the alternating row colours achieved by using the above class names.

My plan was for the class to have an attribute that is assigned the value of one of these 4 values and then handle the font color in the css, getting it to look at the value of the attribute.

As I mentioned, I am very new to this and may have fundamentally misunderstood how to go about this.
 
Hi

HTML:
[b]<td[/b] [maroon]class[/maroon][teal]=[/teal][green][i]"r"[/i][/green][b]>[/b]
Code:
[teal].r[/teal][teal]:[/teal]nth[teal]-[/teal]of[teal]-[/teal]type[teal]([/teal]even[teal])[/teal] [teal]{[/teal] [COLOR=orange]background-color :[/color] [green][i]#E0E0E0[/i][/green] [teal]}[/teal]
[teal].r[/teal][teal]:[/teal]nth[teal]-[/teal]of[teal]-[/teal]type[teal]([/teal]odd[teal])[/teal] [teal]{[/teal] [COLOR=orange]background-color :[/color] [green][i]#D0D0D0[/i][/green] [teal]}[/teal]
Works with Gecko, Presto, KHTML, WebKit. ( For more about pseudo-classes see Selectors Level 3. )

For XML transformation I would ask in forum426. ( But I could be wrong, as I know nothing about them. )

Feherke.
 
I understand the logic just don't recognise it as any form of X/HTML or CSS

The contents of r0 and r1 can be one of 4 values and I want the font color to change for each one
So you want to change the colour dependent of the content value of the td element. not based on attribute of class?


r0 & r1 are class names they cannot have 'contents'.

But that's as far as my knowledge goes, I know nothing of XSL or XSLT , and I can't find a forum on Tek-Tips dedicated to XSL to point you in the right direction either.

I've never transformed an XML document to X/HTML using XSL, maybe TT needs to get a forum dedicated to that?




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Thanks

I understand what you're doing there. Its not actually working for me but I know I am not working in Gecko, Presto etc.

I will probably rewrite my original XML to generate 8 overall classes, 1 for each of the 4 possible values and in each row type.

Thanks for both your help
 
1DMF, I am in exactly the same boat, couldn't find a specific forum for what I was doing. Haven't been able to find much across the net at all actually, which is what makes me think I should go back an rewrite the original files and try and go about it in a more conventional way now I have a bit more knowledge
 
Yes, I did a quick google and couldn't find much, W3C apparently set the spec for XSL in 1999, yet I've never seen it before [lol]

Good luck, it certaily looks interesting, let us know how you get on.

Feherke, looks like I need to investigate CSS3, that syntax has blown my mind! [flip]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
If you are running the code thorough perl, which you obviously are, have the perl script check the value and add an additional css class to that cell. Then you might end up with:
Code:
<td class="r0 c1">value1</td>...
<td class="r1 c2">value2</td>...
<td class="r0 c3">value3</td>...

Code:
.r0 { background-color : #E0E0E0 }
.r1 { background-color : #D0D0D0 }
.c2 { color: black }
.c3 { color:blue } 
{/code]

etc. etc.

Greg
[i]People demand freedom of speech as a compensation for the freedom of thought which they seldom use.[/i] Kierkegaard
 
That was my initial thought, that's how I do it via perl or JavaScript.

But it looks like they want dynamic transformation for XML via XSL.. I'm intrigued, but have way too much on my plate to add antoher thing to learn, well not yet anyway :)

I've nearly ready to launch first ever flash site, got the inlaws rebuild to discuss and design tonight and now my mate has just wont manager contract for a rolling stones tribute band and wants me to do the website!

damn I wish I was a road sweeper sometimes! No one asks you to go round and sweep there garage!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top