color value from xml to backgroundcolor div in xhtml

Here should go questions about transforming XML with XSLT and FOP.
metalhammer
Posts: 26
Joined: Tue Dec 22, 2009 2:40 pm

color value from xml to backgroundcolor div in xhtml

Post by metalhammer » Wed Dec 23, 2009 7:40 pm

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?

alin
Site Admin
Posts: 168
Joined: Thu Dec 24, 2009 11:21 am

Re: color value from xml to backgroundcolor div in xhtml

Post by alin » Thu Dec 24, 2009 12:58 pm

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
Alin Balasa
Software Developer
<oXygen/> XML Editor
http://www.oxygenxml.com

metalhammer
Posts: 26
Joined: Tue Dec 22, 2009 2:40 pm

Re: color value from xml to backgroundcolor div in xhtml

Post by metalhammer » Thu Dec 24, 2009 1:23 pm

Thanks a lot! Works perfectly!

Post Reply