Choose text color for outputted html rendered tabular data through xml with xsl as reference

☆樱花仙子☆ 提交于 2020-01-07 03:06:35

问题


<?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:


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>


来源:https://stackoverflow.com/questions/35622740/choose-text-color-for-outputted-html-rendered-tabular-data-through-xml-with-xsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!