xslt - 使用xsl :choose for updating a single element attribute

标签 xslt

我有以下运行良好的 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/

相关文章:

xslt - 获取元素的下一个同级元素,但位于同一分支上?

javascript - 如何在 xsl 中调用外部 javascript 函数?

xslt - 如何检查元素名称是否包含特定字符串?

c# - 编写 XSLT 样式表 header

java - 如何使用 xsl-fo 渲染 xslt 中 span 标记内的字体颜色和大小等样式?

java - 将绝对文件路径从 java 代码传递到 xslt document()

xml - 在 XSLT 中按月份和年份对 XML 节点进行分组

javascript - 在 xslt 中使用 javascript?

xml - XSLT 如何用 div 包装每 3 个元素?

xml - 在 Perl 或 XSLT 中解析数学公式 XML<-> Latex