I have the following XSLT segment that works fine. It generates a element with a given color according to the @status varibale.
The problem is that it is very unelegant. I am repeating the same values on every xsl:when section.
<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>
As you can see the only thing that is change is the color attribute.
Is there a way for me to have a single task element and have the xsl:choose update only the color attribute?
Thanks in advance...
You could move the choose
inside the task
element, and create just that one attribute node using <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>
Even better:
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With