This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to calculate attribute values with XSL ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]