XSLT - 用类添加类?

Pro*_*cop 14 xslt

使用XSLT时如何将类应用于已有类的元素?我这样做的方式取代了已经存在的类?除现有类外,我如何添加该类?我的代码如下:

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Tom*_*lak 17

另一种方式:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意我在每个属性值之后添加的额外空间.XSLT处理器将自己修剪属性值的尾随空间,因此无需进行复杂的空间处理.


man*_*nji 7

您可以将当前类属性值与新属性值连接起来:

<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>
Run Code Online (Sandbox Code Playgroud)