tags:

views:

33

answers:

2

When using XSLT how do I apply a class to an element which already has a class? The way I'm doing it it replaces the class that is already present? How would I add the class in addition to the existing class? My code is as follows:

<xsl:if test="data[@alias = 'off'] = 1">
    <xsl:attribute name="class">off</xsl:attribute>
</xsl:if>
<xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
</xsl:if>

Thanks.

+3  A: 

The other way around:

<xsl:attribute name="class">
  <xsl:if test="data[@alias = 'off'] = 1">off </xsl:if>
  <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">active </xsl:if>
</xsl:attribute>

Note the extra space I put after every attribute value. The XSLT processor will trim the trailing space from the attribute value on its own, so no need to do complicated space handling.

Tomalak
That's perfect, thanks!
Probocop
+1  A: 

you can concatenate the current class attribute value with the new one:

<xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">
      <xsl:value-of select="concat(@class,' active')"/>
    </xsl:attribute>
</xsl:if>
najmeddine