我有以下运行良好的 XSLT 段。它根据@status varibale生成具有给定颜色的元素。
问题是这样很不优雅。我在每个 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:choose 仅更新颜色属性?
提前致谢...
最佳答案
您可以移动choose
里面task
元素,并使用 <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>
关于xslt - 使用xsl :choose for updating a single element attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17855758/