Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xsl:choose for updating a single element attribute

Tags:

xslt

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

like image 913
Shai Aharoni Avatar asked Mar 24 '23 01:03

Shai Aharoni


2 Answers

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>
like image 54
Ian Roberts Avatar answered Apr 26 '23 13:04

Ian Roberts


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>
like image 41
Michael Kay Avatar answered Apr 26 '23 13:04

Michael Kay