2010-05-26 53 views
12

使用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> 

谢谢。

回答

14

其他各地的道:

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

注意额外的空间我的每个属性值后放。 XSLT处理器将自行修剪属性值的尾部空间,因此不需要进行复杂的空间处理。

+0

这是完美的,谢谢! – Probocop 2010-05-26 09:47:24

+1

请注意,就空间而言,如果以这种方式编写的话,Qt实现将包含换行符。然而,如果你在' 2014-05-14 04:49:13

+0

@Alexis BTW,属性值内的文字换行符在文档再次解析时被归一化为空格。因此,尽管丑陋的外观换行符甚至可以作为令牌分隔符在这里工作。 – Tomalak 2014-05-14 05:03:27

6

,你可以用新的串联当前类属性值:

<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

我试过用Qt的xmlpatterns工具,它不起作用(没有任何反应)。太糟糕了,因为另一种方法,我不能在我的情况下使用! – 2014-08-15 22:58:35