2013-07-25 37 views
3

我有以下XSLT段可以正常工作。它根據@status varibale生成一個給定顏色的元素。使用xsl:選擇更新單個元素屬性

問題是它很不雅觀。我在每個xsl:when部分重複相同的值。

<xsl:template match="Task"> 
     <xsl:choose> 
     <xsl:when test="@status = 'Completed'"> 
     <task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" color="006d0f" borderColor="E1E1E1" /> 
     </xsl:when> 
     <xsl:when test="@status = 'Failed'"> 
     <task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" color="FF0000" borderColor="E1E1E1" /> 
     </xsl:when> 
     <xsl:when test="@status = 'Risk'"> 
     <task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" color="FF9900" borderColor="E1E1E1" /> 
     </xsl:when> 
      <xsl:when test="@status = 'OnGoing'"> 
     <task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" color="14f824" borderColor="E1E1E1" /> 
     </xsl:when> 
     <xsl:otherwise> 
      <task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" color="e8e8e8" borderColor="E1E1E1" /> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

正如你所看到的就是變化是顏色屬性的唯一的事。

有沒有辦法讓我有一個單一的任務元素,並有xsl:選擇只更新顏色屬性?

在此先感謝...

回答

3

您可以移動task元素中的choose,並創建只是一個屬性節點使用<xsl:attribute>

<task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" borderColor="E1E1E1"> 
    <xsl:choose> 
    <xsl:when test="@status = 'Completed'"> 
    <xsl:attribute name="color">006d0f</xsl:attribute> 
    </xsl:when> 
    <xsl:when test="@status = 'Failed'"> 
    <xsl:attribute name="color">FF0000</xsl:attribute> 
    </xsl:when> 
    <xsl:when test="@status = 'Risk'"> 
    <xsl:attribute name="color">FF9900</xsl:attribute> 
    </xsl:when> 
    <!-- etc. etc. --> 
    </xsl:choose> 
</task> 
+0

非常感謝!這正是我需要的。 –

3

更妙的是:

<task name="{@title}" processId="{@resourceId}" start="{@start}" end="{@end}" Id="{@id}" borderColor="E1E1E1"> 
    <xsl:attribute name="color"> 
    <xsl:choose> 
    <xsl:when test="@status = 'Completed'">006d0f</xsl:when> 
    <xsl:when test="@status = 'Failed'">FF0000</xsl:when> 
    <xsl:when test="@status = 'Risk'">FF9900</xsl:when> 
    <!-- etc. etc. --> 
    </xsl:choose> 
</xsl:attribute> 
</task> 
+0

+1:出於某種原因,我的大腦沒有做出最後的飛躍:) –