以xsl作为参考,通过xml为输出的html呈现的表格数据选择文本颜色 [英] Choose text color for outputted html rendered tabular data through xml with xsl as reference

查看:196
本文介绍了以xsl作为参考,通过xml为输出的html呈现的表格数据选择文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?xml version =1.0encoding =UTF-8?>< xsl:stylesheet version =2.0xmlns:xsl =http://www.w3 .org / 1999 / XSL / Transformxmlns:fo =http://www.w3.org/1999/XSL/Format>< xsl:template match =/>< html><头> < style type =text / css> table {table-layout:fixed; width:100%;} td {width:20%;自动换行:打破字;}< /风格>< /头><主体>< H2> RTCP STUB STATUS< / H2><表边界= 1 >< TR BGCOLOR = #9acd32\" ><的第i;存根元器件< /第><的第i;存根名称< /第><的第i;存根操作< /第><的第i;存根版< /第><的第i;存根状态< /第>< / TR><的xsl:for-每个选择= //存根 >< TR>< TD>< XSL:选择= @组分 值的/> < / TD>< TD>< XSL:选择= @名/>值的;< / TD>< TD>< XSL:/>选择= @操作  - 的值; < / TD>< TD>< XSL:value-of的选择= @版本/>< / TD>< TD><的xsl:for-每个选择= 实例/实例 > < xsl:value-of select =@ status/>< xsl:value-of select ='&#160;'/>< / xsl:for-each>< / td> ; < / TR>< /的xsl:for-每个>< /表>< / BODY>< / HTML>< / XSL:模板>< / XSL:样式表>  

基于我上面的xsl,我只想确保每当status列的值为如果其停止显示为红色,则我的输出表格数据正在运行,并以绿色显示。我如何将这部分添加到这个上面的xsl。我尝试了几种方法来做到这一点,但都没有奏效。看起来xsl有自己的方式来显示与颜色有关的输出文本。
在这方面的任何帮助将不胜感激。

示例XML

 <存根> 
<实例>
< instance status =STOPPING/>
< / instances>
< / stub>
< stubs>

谢谢,
Ashley

解决方案

您需要为每个要添加样式的行添加一个类,然后将相应的规则添加到样式表。

>如果你想设置整行的颜色,改变第一行

 < 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:for-each>
< / td>
< / tr>

< tr class ={./ instances / instance / @状态}> 。这会将status作为class属性添加到输出的表格行中。



现在您可以将css样式表修改为

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






如果不想着色整行,只需为状态栏上色,而不是上述内容,您需要更改第一行

 < td> 
< xsl:for-each select =instances / instance>
< xsl:value-of select =@ status/>
< / xsl:for-each>
< / td>

< td class ={./ instances / instance / @状态}> 并将css样式表修改为

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


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

解决方案

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>

这篇关于以xsl作为参考,通过xml为输出的html呈现的表格数据选择文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆