Choose text color for outputted html rendered tabu

2019-09-11 15:07发布

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<head>
   <style type="text/css">
    table {table-layout: fixed; width: 100%;}
    td {width: 20%; word-wrap: break-word;}
   </style>
</head>
<body>
<h2>RTCP STUB STATUS</h2>
<table border="1">
<tr bgcolor="#9acd32" >
<th>Stub Component</th>
<th>Stub Name</th>
<th>Stub Operation</th>
<th>Stub Version</th>
<th>Stub Status</th>
</tr>
<xsl:for-each select="//stub">
<tr>
<td><xsl:value-of select="@component" /></td>
<td><xsl:value-of select="@name" /></td>
<td><xsl:value-of select="@operation" /></td>
<td><xsl:value-of select="@version" /></td>
<td><xsl:for-each select="instances/instance">
<xsl:value-of select="@status"/>
<xsl:value-of select="'&#160;'"/>
</xsl:for-each>
</td> 
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Based on my above xsl, i just want to make sure that whenever the value for status column for my output tabular data is Running its displayed in green color, if its Stopping it gets displayed as red. How do i add that part to this above xsl. I tried several ways to do this but none worked. It seems xsl has its own way to display output text pertaining to color. Any help in this regard would be greatly appreciated.

Sample XML

<stubs> 
    <stub component="ChannelInquiry_Binding_HTTP_v2" name="getAllChannelAvailabilityStub" operation="getAllChannelAvailability" version="26.7"> 
        <instances>
            <instance status="STOPPING"/> 
        </instances> 
    </stub>
<stubs>

Thanks, Ashley

1条回答
Rolldiameter
2楼-- · 2019-09-11 16:02

You need to add a class to each of your rows to be styled, and then add the appropriate rules to the stylesheet.

If you wish to set the color of the entire row, change the first line of

<tr>
    <td><xsl:value-of select="@component" /></td>
    <td><xsl:value-of select="@name" /></td>
    <td><xsl:value-of select="@operation" /></td>
    <td><xsl:value-of select="@version" /></td>
    <td>
        <xsl:for-each select="instances/instance">
            <xsl:value-of select="@status"/>
            <xsl:value-of select="'&#160;'"/>
        </xsl:for-each>
    </td>
</tr>

to <tr class="{./instances/instance/@status}">. This will add the status as a class attribute to your outputted table rows.

Now you can modify the css stylesheet to

<style type="text/css">
    table {table-layout: fixed; width: 100%;}
    td {width: 20%; word-wrap: break-word;}
    tr.Running {color: green;}
    tr.Stopping {color: red;}
</style>

If instead of coloring the entire row, you wish just to color the status column, instead of the above, you need to change the first line of

<td>
    <xsl:for-each select="instances/instance">
        <xsl:value-of select="@status"/>
        <xsl:value-of select="'&#160;'"/>
    </xsl:for-each>
</td>

to <td class="{./instances/instance/@status}"> and modify the css stylesheet to

<style type="text/css">
    table {table-layout: fixed; width: 100%;}
    td {width: 20%; word-wrap: break-word;}
    td.Running {color: green;}
    td.Stopping {color: red;}
</style>
查看更多
登录 后发表回答