最近遇到了一个场景:
1、一个客户端软件,需要对其中用户的个性化偏好进行记录,但是同一个计算机可能会有多人使用这个软件,因此软件需要记录每个人的偏好
因此我设计了下面这个XML文件(PersonalizedConfig.xml),用来保存偏好:
- <?xml version="1.0" encoding="gb2312"?>
- <?xml-stylesheet type='text/xsl' href='PersonalizedConfig.xslt'?>
- <!DOCTYPE UserConfig PUBLIC "-//Tsybius//UserConfig//EN" "PersonalizedConfig.dtd">
- <UserConfig Title="用户个性化配置参数表">
- <User MemCode="10000001" OperId="10006">
- <Config Key="Config1" Value="testConfigValue1" />
- <Config Key="Config2" Value="testConfigValue2" />
- <Config Key="Config3" Value="testConfigValue3" />
- <Config Key="Config4" Value="testConfigValue4" />
- <Config Key="Config5" Value="testConfigValue5" />
- </User>
- <User MemCode="10000001" OperId="10008">
- <Config Key="Config1" Value="testConfigValueA" />
- <Config Key="Config2" Value="testConfigValueB" />
- <Config Key="Config3" Value="testConfigValueC" />
- <Config Key="Config4" Value="testConfigValueD" />
- <Config Key="Config5" Value="testConfigValueE" />
- </User>
- </UserConfig>
其中每个User元素是一个用户,MemCode和OperId属性用作该用户的唯一标识(即说明该用户是哪个机构下的哪个操作员)。每个用户下面,都有多个Config元素,其中包含了Key和Value两个属性,每一个Config元素说明该用户一个配置项的设置情况。
相关文档类型定义文件(PersonalizedConfig.dtd)代码如下:
相关样式文件(PersonalizedConfig.xslt)代码如下:
- <?xml version="1.0" encoding="gb2312"?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <!--用户个性化配置文件XSLT样式表-->
- <xsl:output method="html"/>
- <xsl:template match="/">
- <html>
- <head>
- <Meta content="zh-cn" http-equiv="Content-Language"/>
- <Meta content="text/html; charset=utf-16" http-equiv="Content-Type"/>
- <link type="text/css" rel="stylesheet" href="PersonalizedConfig.css"/>
- <title><xsl:value-of select="UserConfig/@Title" /></title>
- <script type="text/javascript" language="javascript">
- <xsl:text disable-output-escaping="yes">
- <![CDATA[
- //自动刷新
- function myrefresh()
- {
- window.location.reload();
- }
- setTimeout('myrefresh()',3000); //在这里指定刷新间隔,单位毫秒
- ]]>
- </xsl:text>
- </script>
- </head>
- <body>
- <div id="content">
- <br />
- <strong><font color="red"><xsl:value-of select="UserConfig/@Title" /></font></strong><br />
- <hr />
- <table>
- <tr>
- <th>用户编号</th>
- <th>操作员代码</th>
- <th>配置项1</th>
- <th>配置项2</th>
- <th>配置项3</th>
- <th>配置项4</th>
- <th>配置项5</th>
- </tr>
- <xsl:for-each select="UserConfig/User">
- <tr>
- <td class="textCentered"><xsl:value-of select="@MemCode" /></td>
- <td class="textCentered"><xsl:value-of select="@OperId" /></td>
- <xsl:for-each select="Config">
- <td class="textCentered"><xsl:value-of select="@Value"/></td>
- </xsl:for-each>
- </tr>
- </xsl:for-each>
- </table>
- </div>
- </body>
- </html>
- </xsl:template>
- </xsl:stylesheet>
注意在这个XSLT文件中,包含了自动刷新相关的代码(3秒刷一次),这样在用浏览器打开XML并的时候,就可以看到XML文档的最新数据变更情况了。
XSLT文件使用到的CSS样式表文件(PersonalizedConfig.css)代码如下:
- /* Body style,for the entire document */
- body
- {
- background: #F3F3F4;
- color: #1E1E1F;
- font-family: "Segoe UI",Tahoma,Geneva,Verdana,sans-serif;
- padding: 0;
- margin: 0;
- }
- /* Header1 style,used for the main title */
- h1
- {
- padding: 10px 0px 10px 10px;
- font-size: 21pt;
- background-color: #E2E2E2;
- border-bottom: 1px #C1C1C2 solid;
- color: #201F20;
- margin: 0;
- font-weight: normal;
- }
- /* Table styles */
- table
- {
- border-spacing: 0 0;
- border-collapse: collapse;
- font-size: 10pt;
- }
- table th
- {
- background: #3399FF;
- text-align: center;
- text-decoration: none;
- font-weight: normal;
- padding: 3px 6px 3px 6px;
- width:200px;
- }
- table td
- {
- vertical-align: top;
- text-align: center;
- padding: 3px 6px 3px 6px;
- margin: 0px;
- border: 1px solid #3399FF;
- /*background: #66FF33;*/
- }
- .textCentered
- {
- text-align: center;
- }
- #content
- {
- padding: 0px 12px 40px 40px;
- }
在IE8.0中打开上面的XML文档,展示效果如下:
END