Page 1 of 1

color value from xml to backgroundcolor div in xhtml

Posted: Wed Dec 23, 2009 7:40 pm
by metalhammer
Hi,

I have RGB value's in my XML file, they look like this:

Code: Select all


<egInk:r>.3218039274</egInk:r>
<egInk:g>.4258823395</egInk:g>
<egInk:b>.4894117713</egInk:b>
I want a square in my html file with those RGB value's as color.
I've tried this:

Code: Select all


<div class="kleurvak" style="background-color:rgb(<xsl:value-of select="round(egInk:r*256)"/>,<xsl:value-of select="round(egInk:g*256)"/>,<xsl:value-of select="round(egInk:b*256)"/>);">

</div>
But I can't put an xslt element in an attribute of a div element.
Is there an other way to do this?

Re: color value from xml to backgroundcolor div in xhtml

Posted: Thu Dec 24, 2009 12:58 pm
by alin
Hello,

There are two ways to obtain what you want.
Instead of directly declaring the "div" element and its attributes in the stylesheet, you can generate them using the xsl:element and xsl:attribute elements (http://www.w3.org/TR/xslt#section-Creat ... sl:element). Here is an example of how you can do this:

Code: Select all


    <xsl:element name="div">
<xsl:attribute name="class">kleurvak</xsl:attribute>
<xsl:attribute name="style">
<xsl:text>background-color:rgb(</xsl:text>
<xsl:value-of select="round(egInk:r*256)"/>
<xsl:text> , </xsl:text>
<xsl:value-of select="round(egInk:g*256)"/>
<xsl:text> , </xsl:text>
<xsl:value-of select="round(egInk:b*256)"/>
<xsl:text>)</xsl:text>
</xsl:attribute>
</xsl:element>
Another way to obtain the result is by using the attribute value templates feature of XSLT (http://www.w3.org/TR/xslt#attribute-value-templates). This means you can use an xpath expression inside an attribute value by surrounding it with curly braces ({}). In your case it will look something like this:

Code: Select all


    <div class="kleurvak" style="background-      color:rgb({round(egInk:r*256)},{round(egInk:g*256)},{round(egInk:b*256)})">
</div>
Let us know if you have further questions regarding this issue.

Regards,
Alin

Re: color value from xml to backgroundcolor div in xhtml

Posted: Thu Dec 24, 2009 1:23 pm
by metalhammer
Thanks a lot! Works perfectly!