This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How to calculate attribute values with XSL ?
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: stig dot garberg at no dot abb dot com
- Cc: XSL-List at lists dot mulberrytech dot com
- Date: Tue, 8 Jan 2002 08:56:33 +0000
- Subject: Re: [xsl] How to calculate attribute values with XSL ?
- Organization: Jeni Tennison Consulting Ltd
- References: <C1256B3B.002E1972.00@noeie.abb.no>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Stig,
> I'm working with working with XSL for HTML output. A problem that
> has occured several times is that I want to calculate the values of
> HTML attributes based on input XML values, but I'm not able to find
> a sensible way to do it.
You can set the value of an attribute in two ways using XSLT - with
attribute value templates or with xsl:attribute. Attribute value
templates involve using {}s inside the attribute value - anything
within those {}s is interpreted as an expression, evaluated, and its
string value inserted. For example, you could do:
<xsl:variable name="bgcolor">
<xsl:choose>
<xsl:when test="state = 'on' and ack = 'off'">#FF0000</xsl:when>
<xsl:when test="state = 'on' and ack = 'on'">#008000</xsl:when>
...
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$bgcolor}">
...
</tr>
Alternatively, you can use xsl:attribute just after opening the
element. The name attribute specifies the name of the attribute, and
the content of the xsl:attribute instruction gives the value for the
attribute, which you can calculate using XSLT instructions:
<tr>
<xsl:attribute name="bgcolor">
<xsl:choose>
<xsl:when test="state = 'on' and ack = 'off'">#FF0000</xsl:when>
<xsl:when test="state = 'on' and ack = 'on'">#008000</xsl:when>
...
</xsl:choose>
</xsl:attribute>
...
</tr>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list