使用XSLT统计存储在XML中的日志信息

前端之家收集整理的这篇文章主要介绍了使用XSLT统计存储在XML中的日志信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天研究了下VS在读取低版本项目时对其进行升级的迁移报告“UpgradeLog.XML”,这个报告是一个XML文件,但是加载了一个XSLT文件“UpgradeReport.xslt”。这个XSLT文件除了用于逻辑计算的部分外,还包含了一些javascript代码,并引用了一个CSS文件“UpgradeReport.css”用来配置样式。

于是我照猫画虎,用XSLT实现了一个简易版的XML日志统计界面,只实现了统计各类日志的功能

“LogList.xml”的内容如下:

  1. <?xml version="1.0" encoding="gb2312"?>
  2. <?xml-stylesheet type='text/xsl' href='Resources/LogList.xslt'?>
  3. <!--LogLevel:事件等级0-3,LogItem:报警项,LogTime:发生时间,Description:报警信息-->
  4. <LogList>
  5. <Log LogLevel="0" LogItem="Zhang" Description="PERFECT1"/>
  6. <Log LogLevel="0" LogItem="Zhang" Description="PERFECT2"/>
  7. <Log LogLevel="1" LogItem="Zhang" Description="INFO1"/>
  8. <Log LogLevel="1" LogItem="Zhang" Description="INFO2"/>
  9. <Log LogLevel="1" LogItem="Zhang" Description="INFO3"/>
  10. <Log LogLevel="2" LogItem="Zhang" Description="WARNING1"/>
  11. <Log LogLevel="3" LogItem="Zhang" Description="ERROR1"/>
  12. <Log LogLevel="3" LogItem="Zhang" Description="ERROR2"/>
  13. <Log LogLevel="3" LogItem="Zhang" Description="ERROR3"/>
  14. <Log LogLevel="3" LogItem="Zhang" Description="ERROR4"/>
  15. <Log LogLevel="0" LogItem="Lee" Description="PERFECT1"/>
  16. <Log LogLevel="0" LogItem="Lee" Description="PERFECT2"/>
  17. <Log LogLevel="1" LogItem="Wang" Description="INFO1"/>
  18. <Log LogLevel="1" LogItem="Wang" Description="INFO2"/>
  19. <Log LogLevel="2" LogItem="Wang" Description="WARNING1"/>
  20. <Log LogLevel="1" LogItem="Zhao" Description="INFO1"/>
  21. </LogList>

根节点LogList下,有多个Log标签,LogLevel从0到3分别代表“正常”、“信息”、“警告”、“错误”,LogItem是一个标识,同一个LogItem的日志统计到一起,Description是一个对单条日志的描述,在统计中没有实际意义。

新建一个文件夹Resource,里面我用画图画了4张图片

1)正常:0_PERFECT.png

2)信息:1_INFO.png

3)警告:2_WARNING.png

4)错误:3_ERROR.png

然后创建一个XSLT文件,取名“LogList.xslt”:

  1. <?xml version="1.0" encoding="gb2312"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  3. <!-- Keys -->
  4. <xsl:key name="LogItemKey" match="Log" use="@LogItem"/>
  5.  
  6. <!-- Intermediate Templates -->
  7. <xsl:template match="LogList" mode="logoverviewXML">
  8. <LogItems>
  9. <xsl:for-each select="Log[generate-id(.) = generate-id(key('LogItemKey',@LogItem))]">
  10. <LogItem>
  11. <xsl:variable name="pNode" select="current()"/>
  12. <xsl:variable name="errorCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=3])"/>
  13. <xsl:variable name="warningCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=2])"/>
  14. <xsl:variable name="infoCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=1])"/>
  15. <xsl:variable name="perfectCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=0])"/>
  16. <xsl:attribute name="Status">
  17. <xsl:choose>
  18. <xsl:when test="$errorCount &gt; 0">Error</xsl:when>
  19. <xsl:when test="$warningCount &gt; 0">Warning</xsl:when>
  20. <xsl:when test="$infoCount &gt; 0">Info</xsl:when>
  21. <xsl:when test="$perfectCount &gt; 0">Perfect</xsl:when>
  22. <xsl:otherwise>Perfect</xsl:otherwise>
  23. </xsl:choose>
  24. </xsl:attribute>
  25. <xsl:attribute name="LogItem">
  26. <xsl:value-of select="@LogItem"/>
  27. </xsl:attribute>
  28. <xsl:attribute name="ErrorCount">
  29. <xsl:value-of select="$errorCount"/>
  30. </xsl:attribute>
  31. <xsl:attribute name="WarningCount">
  32. <xsl:value-of select="$warningCount"/>
  33. </xsl:attribute>
  34. <xsl:attribute name="InfoCount">
  35. <xsl:value-of select="$infoCount"/>
  36. </xsl:attribute>
  37. <xsl:attribute name="PerfectCount">
  38. <xsl:value-of select="$perfectCount"/>
  39. </xsl:attribute>
  40. <xsl:attribute name="TotalCount">
  41. <xsl:value-of select="$errorCount + $warningCount + $infoCount + $perfectCount"/>
  42. </xsl:attribute>
  43. </LogItem>
  44. </xsl:for-each>
  45. </LogItems>
  46. </xsl:template>
  47.  
  48. <!-- LogItem Overview template -->
  49. <xsl:template match="LogItems" mode="logoverview">
  50.  
  51. <table>
  52. <tr>
  53. <th></th>
  54. <th>项目</th>
  55. <th>错误</th>
  56. <th>警告</th>
  57. <th>信息</th>
  58. <th>正常</th>
  59. </tr>
  60.  
  61. <xsl:for-each select="LogItem">
  62. <tr>
  63. <td>
  64. <img width="16" height="16">
  65. <xsl:attribute name="src">
  66. <xsl:choose>
  67. <xsl:when test="@Status = 'Error'">Resources\3_ERROR.png</xsl:when>
  68. <xsl:when test="@Status = 'Warning'">Resources\2_WARNING.png</xsl:when>
  69. <xsl:when test="@Status = 'Info'">Resources\1_INFO.png</xsl:when>
  70. <xsl:when test="@Status = 'Perfect'">Resources\0_PERFECT.png</xsl:when>
  71. </xsl:choose>
  72. </xsl:attribute>
  73. <xsl:attribute name="alt">
  74. <xsl:value-of select="@Status"/>
  75. </xsl:attribute>
  76. </img>
  77. </td>
  78. <td>
  79. <xsl:value-of select="@LogItem"/>
  80. </td>
  81. <td class="textCentered">
  82. <xsl:value-of select="@ErrorCount"/>
  83. </td>
  84. <td class="textCentered">
  85. <xsl:value-of select="@WarningCount"/>
  86. </td>
  87. <td class="textCentered">
  88. <xsl:value-of select="@InfoCount"/>
  89. </td>
  90. <td class="textCentered">
  91. <xsl:value-of select="@PerfectCount"/>
  92. </td>
  93. </tr>
  94. </xsl:for-each>
  95. </table>
  96. </xsl:template>
  97.  
  98. <!-- Document,matches "LogList" -->
  99. <xsl:template match="LogList">
  100. <html>
  101. <head>
  102. <Meta content="en-us" http-equiv="Content-Language"/>
  103. <Meta content="text/html; charset=utf-16" http-equiv="Content-Type"/>
  104. <link type="text/css" rel="stylesheet" href="Resources\LogList.css"/>
  105. <title>
  106. 日志统计报告
  107. </title>
  108. </head>
  109. <body>
  110. <h1>
  111. 日志统计报告
  112. </h1>
  113. <div id="content">
  114. <br/>
  115. <xsl:variable name="logoverview">
  116. <xsl:apply-templates select="self::node()" mode="logoverviewXML"/>
  117. </xsl:variable>
  118. <div id="overview">
  119. <xsl:apply-templates select="msxsl:node-set($logoverview)/*" mode="logoverview"/>
  120. </div>
  121. </div>
  122. </body>
  123. </html>
  124. </xsl:template>
  125. </xsl:stylesheet>

这个文件要引用一个样式表文件,创建“LogList.css”,也放到Resources目录下,代码如下:

  1. /* Body style,for the entire document */
  2. body
  3. {
  4. background: #F3F3F4;
  5. color: #1E1E1F;
  6. font-family: "Segoe UI",Tahoma,Geneva,Verdana,sans-serif;
  7. padding: 0;
  8. margin: 0;
  9. }
  10.  
  11. /* Header1 style,used for the main title */
  12. h1
  13. {
  14. padding: 10px 0px 10px 10px;
  15. font-size: 21pt;
  16. background-color: #E2E2E2;
  17. border-bottom: 1px #C1C1C2 solid;
  18. color: #201F20;
  19. margin: 0;
  20. font-weight: normal;
  21. }
  22.  
  23. /* Table styles */
  24. table
  25. {
  26. border-spacing: 0 0;
  27. border-collapse: collapse;
  28. font-size: 10pt;
  29. }
  30.  
  31. table th
  32. {
  33. background: #E7E7E8;
  34. text-align: left;
  35. text-decoration: none;
  36. font-weight: normal;
  37. padding: 3px 6px 3px 6px;
  38. }
  39.  
  40. table td
  41. {
  42. vertical-align: top;
  43. padding: 3px 6px 5px 5px;
  44. margin: 0px;
  45. border: 1px solid #E7E7E8;
  46. background: #F7F7F8;
  47. }
  48.  
  49. /* Center text,used in the over views cells that contain message level counts */
  50. .textCentered
  51. {
  52. text-align: center;
  53. }
  54.  
  55. /* The message cells in message tables should take up all avaliable space */
  56. .messageCell
  57. {
  58. width: 100%;
  59. }
  60.  
  61. /* Padding around the content after the h1 */
  62. #content
  63. {
  64. padding: 0px 12px 12px 12px;
  65. }

(这个CSS文件删减自上面所说的UpgradeReport.css)

现在再双击文件“LogList.xml”,就可以在浏览器总看到下面的效果了:

附:本文中文件结构图

  1. C:\Users\Tsybius\Desktop\XLST_Test
  2. |
  3. |-Resources
  4. | |
  5. | |-0_PERFECT.png
  6. | |-1_INFO.png
  7. | |-2_WARNING.png
  8. | |-3_ERROR.png
  9. | |
  10. | |-LogList.css
  11. | |-LogList.xslt
  12. |
  13. |-LogList.xml

END

猜你在找的XML相关文章