开发者

XSLT - Adding a class to something with a class?

When using XSLT how do I apply a class to an ele开发者_如何学编程ment 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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜