如何根据内容不同显示不同颜色

lidopaul 2003-10-09 05:50:05
如下代码中,当“EventType=信息”用黑色表示,“EventType=警告”用蓝色表示,“EventType=错误”用红色表示,xml中的数据是用vc加进去的,本来我在程序中想用<font color=ff0000>错误</font>来区分,可是写到文件中“<”和“>”却变成非html代码。请问是否有方法纠正。或者可以用xsl来定义显示。谢谢。

//////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet href="Log.xsl" type="text/xsl" ?>
<LogFile>
<AppName>LogApp</AppName>
<Timestamp>1065773616.984</Timestamp>
<Date>2003-10-10</Date>
<Time>16:13:36</Time>
<LogItem>
<EventType>信息</EventType>
<Date>2003-10-09</Date>
<Time>15:47:40</Time>
<Event>OnNewDocument()</Event>
<comment>ÎÞ</comment>
<Source>Main Program</Source>
</LogItem>
<LogItem>
<EventType>警告</EventType>
<Date>2003-10-09</Date>
<Time>15:47:43</Time>
<Event>OnNewDocument()</Event>
<comment>ÎÞ</comment>
<Source>Main Program</Source>
</LogItem>
<LogItem>
<EventType>错误</EventType>
<Date>2003-10-09</Date>
<Time>15:47:45</Time>
<Event>~CLogAppDoc()</Event>
<comment>ÎÞ</comment>
<Source>Main Program</Source>
</LogItem>
</LogFile>
...全文
623 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lidopaul 2003-10-10
  • 打赏
  • 举报
回复
我的完整代码
<?xml version="1.0" encoding="GB2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title> LogApp </title>
<style>
body,BlueIdea,team,blue_ID,blue_name,blue_text,blue_time,blue_class{ font: 13px "宋体", "Arial", "Times New Roman"; }
table { font-size: 13px; border: 0px double; border-color: #99CC99 #99CC99 #CCCCCC #CCCCCC; cellpadding:3;cellspacing:3; bgcolor:#eeeeee; text-decoration: blink}
span { font-size: 13px; color: red; }
</style>
<script>
function searchtext(FilterValue,FilterItem)
{
stylesheet=document.XSLDocument;
source=document.XMLDocument;
sortField=document.XSLDocument.selectNodes("//@select");
if ( FilterValue != "" )
{
sortField[1].value="LogItem[EventType='" + FilterValue + "']";

if(FilterItem == "EventType")
{
sortField[1].value="LogItem[EventType='"+FilterValue+"']";
}
if(FilterItem == "Date")
{
sortField[1].value="LogItem[Date='"+FilterValue+"']";
}
if(FilterItem == "Time")
{
sortField[1].value="LogItem[Time='"+FilterValue+"']";
}
if(FilterItem == "Event")
{
sortField[1].value="LogItem[Event='"+FilterValue+"']";
}
if(FilterItem == "comment")
{
sortField[1].value="LogItem[comment='"+FilterValue+"']";
}
if(FilterItem == "Source")
{
sortField[1].value="LogItem[Source='"+FilterValue+"']";
}

Layer1.innerHTML=source.documentElement.transformNode(stylesheet);
}
else
{
// alert("请输入筛选条件!");
document.location=document.location;
}
}

function XmlSort(SortItem)
{
stylesheet=document.XSLDocument;
source=document.XMLDocument;
sortField=document.XSLDocument.selectSingleNode("//@order-by");
sortField.value=SortItem;
Layer1.innerHTML=source.documentElement.transformNode(stylesheet);
}
</script>
</head>
<body>
<p align="center"><span>LogApp</span></p>
<div id="Layer1" name="Layer1">
<xsl:apply-templates select="LogFile" />
</div>

<p align="center"><span>
<table align="center" cellpadding="0" cellspacing="0" border="0" >
<tr>
<td>
<span >请输入筛选条件 : </span>

<select name='Filter' >
<option value="EventType" > 类型 </option>
<option value="Date"> 日期 </option>
<option value="Time"> 时间 </option>
<option value="Event"> 事件 </option>
<option value="comment"> 信息 </option>
<option value="Source"> 来源 </option>
</select>
= <input type="text" name="searchtext" size="20" maxlength="100" />
<input type="button" class="button" onClick="searchtext(document.all.searchtext.value,document.all.Filter.value)" value="Search" name="button" />
<span >(无条件输入则显示全部内容)</span>
</td>
</tr>
</table>
</span></p>

<p align="center"><span>
<br/>LogApp by wyf <<a href="mailto:yfwang@neusoft.com">yfwang@neusoft.com</a>>
</span></p>
</body>
</html>
</xsl:template>
<xsl:template match="LogFile">
<table width="500" border="1" align="center" cellpadding="1" cellspacing="1" bordercolordark="#ffffff" bordercolorlight="#ADAAAD">
<tr bgcolor="#FFCC99" align="center">
<td style="cursor:s-resize" onClick="XmlSort('EventType')" ><b>类型</b> </td>
<td style="cursor:s-resize" onClick="XmlSort('Date')" ><b>日期</b></td>
<td style="cursor:s-resize" onClick="XmlSort('Time')" ><b>时间</b></td>
<td style="cursor:s-resize" onClick="XmlSort('Event')" ><b>事件</b></td>
<td style="cursor:s-resize" onClick="XmlSort('comment')" ><b>描述信息</b></td>
<td style="cursor:s-resize" onClick="XmlSort('Source')" ><b>来源</b></td>
</tr>

<xsl:apply-templates select="LogItem" order-by="Date"/>

</table>
</xsl:template>
<xsl:template match="LogItem">
<tr >
<td bgcolor="#ffffff" valign="top" align="center">

<font>
<nobr>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="EventType='信息'">#ff0000</xsl:when>
<xsl:when test="EventType='警告'">#0000ff</xsl:when>
<xsl:otherwise>#FF0000</xsl:otherwise>
</xsl:choose>
</xsl:attribute>

<xsl:value-of select="EventType" />
</nobr>
</font>

</td>
<td bgcolor="#ffffff" valign="top" align="center"><nobr><xsl:value-of select="Date" /></nobr></td>
<td bgcolor="#ffffff" valign="top" align="center"><nobr><xsl:value-of select="Time" /></nobr></td>
<td bgcolor="#ffffff" valign="top"> <xsl:value-of select="Event" /></td>
<td bgcolor="#ffffff" valign="top"> <xsl:value-of select="comment" /></td>
<td bgcolor="#ffffff" valign="top"> <xsl:value-of select="Source" /></td>
</tr>
</xsl:template>
<xsl:template match="EventType">
<td bgcolor="#eeeeee">
<xsl:value-of />
</td>
</xsl:template>
<xsl:template match="Date">
<td>
<xsl:value-of />
</td>
</xsl:template>
<xsl:template match="Time">
<td>
<xsl:value-of />
</td>
</xsl:template>
<xsl:template match="Event">
<td>
<xsl:value-of />
</td>
</xsl:template>
<xsl:template match="comment">
<td>
<xsl:value-of />
</td>
</xsl:template>
<xsl:template match="Source">
<td>
<xsl:value-of />
</td>
</xsl:template>
</xsl:stylesheet>
lidopaul 2003-10-10
  • 打赏
  • 举报
回复
楼上的兄弟,我是用http://www.w3.org/TR/WD-xsl解释的,用你的http://www.w3.org/1999/XSL/Transform解释我的xml无法显示了,不知我的那个解释器中是否由你这样的功能,从那里能查到
lidopaul 2003-10-10
  • 打赏
  • 举报
回复
ok了,xsl中如此写即可
<xsl:when test=".[EventType='ÐÅÏ¢']">#ff0000</xsl:when>
<xsl:when test=".[EventType='¾¯¸æ']">#0000ff</xsl:when>
<xsl:otherwise>#FF0000</xsl:otherwise>
孟子E章 2003-10-10
  • 打赏
  • 举报
回复
另外,你的xml里的<comment>ÎÞ</comment>
应该换成
<comment>&Icirc;&THORN;</comment>
孟子E章 2003-10-10
  • 打赏
  • 举报
回复
<xsl:attribute name="color"><xsl:choose><xsl:when test="EventType[.='信息']">#ff0000</xsl:when><xsl:when test="EventType[.='警告']">#0000ff</xsl:when><xsl:otherwise>#FF0000</xsl:otherwise></xsl:choose></xsl:attribute>
孟子E章 2003-10-09
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/LogFile/LogItem">
<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="EventType='信息'">#000000</xsl:when>
<xsl:when test="EventType='警告'">#0000ff</xsl:when>
<xsl:otherwise>#FF0000</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="EventType"/>
</font>
<xsl:value-of select="Source"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
lidopaul 2003-10-09
  • 打赏
  • 举报
回复
up myself
lidopaul 2003-10-09
  • 打赏
  • 举报
回复
up myself
lidopaul 2003-10-09
  • 打赏
  • 举报
回复
不好意思,楼上的兄弟,你的意思我没弄明白,我对xml不熟,只是要用做日志故而改了一下别人的代码。能否详细说明一下,最好有例子,谢谢!
月光易水 2003-10-09
  • 打赏
  • 举报
回复
try:
<xsl:value-of select="节点名称" />
==>
<xsl:value-of select="节点名称" disable-output-escaping="yes" />

:_)

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧