使用XSLT統計存儲在XML中的日誌信息

今天研究了下VS在讀取低版本項目時對其進行升級的遷移報告「UpgradeLog.XML」,這個報告是一個XML文件,可是加載了一個XSLT文件「UpgradeReport.xslt」。這個XSLT文件除了用於邏輯計算的部分外,還包含了一些javascript代碼,並引用了一個CSS文件「UpgradeReport.css」用來配置樣式。javascript

因而我照貓畫虎,用XSLT實現了一個簡易版的XML日誌統計界面,只實現了統計各種日誌的功能。css

「LogList.xml」的內容以下:html

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type='text/xsl' href='Resources/LogList.xslt'?>
<!--LogLevel:事件等級0-3,LogItem:報警項,LogTime:發生時間,Description:報警信息-->
<LogList>
  <Log LogLevel="0" LogItem="Zhang" Description="PERFECT1"/>
  <Log LogLevel="0" LogItem="Zhang" Description="PERFECT2"/>
  <Log LogLevel="1" LogItem="Zhang" Description="INFO1"/>
  <Log LogLevel="1" LogItem="Zhang" Description="INFO2"/>
  <Log LogLevel="1" LogItem="Zhang" Description="INFO3"/>
  <Log LogLevel="2" LogItem="Zhang" Description="WARNING1"/>
  <Log LogLevel="3" LogItem="Zhang" Description="ERROR1"/>
  <Log LogLevel="3" LogItem="Zhang" Description="ERROR2"/>
  <Log LogLevel="3" LogItem="Zhang" Description="ERROR3"/>
  <Log LogLevel="3" LogItem="Zhang" Description="ERROR4"/>
  <Log LogLevel="0" LogItem="Lee" Description="PERFECT1"/>
  <Log LogLevel="0" LogItem="Lee" Description="PERFECT2"/>
  <Log LogLevel="1" LogItem="Wang" Description="INFO1"/>
  <Log LogLevel="1" LogItem="Wang" Description="INFO2"/>
  <Log LogLevel="2" LogItem="Wang" Description="WARNING1"/>
  <Log LogLevel="1" LogItem="Zhao" Description="INFO1"/>
</LogList>

 

根節點LogList下,有多個Log標籤,LogLevel從0到3分別表明「正常」、「信息」、「警告」、「錯誤」,LogItem是一個標識,同一個LogItem的日誌統計到一塊兒,Description是一個對單條日誌的描述,在統計中沒有實際意義。java

新建一個文件夾Resource,裏面我用畫圖畫了4張圖片:node

1)正常:0_PERFECT.png瀏覽器

2)信息:1_INFO.pngapp

3)警告:2_WARNING.pngui

4)錯誤:3_ERROR.pngspa

而後建立一個XSLT文件,取名「LogList.xslt」:日誌

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  
  <!-- Keys -->
  <xsl:key name="LogItemKey" match="Log" use="@LogItem"/>

  <!-- Intermediate Templates -->
  <xsl:template match="LogList" mode="LogOverviewXML">
    <LogItems>
      <xsl:for-each select="Log[generate-id(.) = generate-id(key('LogItemKey', @LogItem))]">
        <LogItem>
          
          <xsl:variable name="pNode" select="current()"/>
          <xsl:variable name="errorCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=3])"/>
          <xsl:variable name="warningCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=2])"/>
          <xsl:variable name="infoCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=1])"/>
          <xsl:variable name="perfectCount" select="count(../Log[@LogItem = current()/@LogItem and @LogLevel=0])"/>
          
          <xsl:attribute name="Status">
            <xsl:choose>
              <xsl:when test="$errorCount &gt; 0">Error</xsl:when>
              <xsl:when test="$warningCount &gt; 0">Warning</xsl:when>
              <xsl:when test="$infoCount &gt; 0">Info</xsl:when>
              <xsl:when test="$perfectCount &gt; 0">Perfect</xsl:when>
              <xsl:otherwise>Perfect</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          
          <xsl:attribute name="LogItem">
            <xsl:value-of select="@LogItem"/>
          </xsl:attribute>
          
          <xsl:attribute name="ErrorCount">
            <xsl:value-of select="$errorCount"/>
          </xsl:attribute>
          <xsl:attribute name="WarningCount">
            <xsl:value-of select="$warningCount"/>
          </xsl:attribute>
          <xsl:attribute name="InfoCount">
            <xsl:value-of select="$infoCount"/>
          </xsl:attribute>
          <xsl:attribute name="PerfectCount">
            <xsl:value-of select="$perfectCount"/>
          </xsl:attribute>
          <xsl:attribute name="TotalCount">
            <xsl:value-of select="$errorCount + $warningCount + $infoCount + $perfectCount"/>
          </xsl:attribute>
          
        </LogItem>
      </xsl:for-each>
    </LogItems>
  </xsl:template>

  <!-- LogItem Overview template -->
  <xsl:template match="LogItems" mode="LogOverview">

    <table>
      <tr>
        <th></th>
        <th>項目</th>
        <th>錯誤</th>
        <th>警告</th>
        <th>信息</th>
        <th>正常</th>
      </tr>

      <xsl:for-each select="LogItem">
      
        <tr>
          <td>
            <img width="16" height="16">
              <xsl:attribute name="src">
                <xsl:choose>
                  <xsl:when test="@Status = 'Error'">Resources\3_ERROR.png</xsl:when>
                  <xsl:when test="@Status = 'Warning'">Resources\2_WARNING.png</xsl:when>
                  <xsl:when test="@Status = 'Info'">Resources\1_INFO.png</xsl:when>
                  <xsl:when test="@Status = 'Perfect'">Resources\0_PERFECT.png</xsl:when>
                </xsl:choose>
              </xsl:attribute>
              <xsl:attribute name="alt">
                <xsl:value-of select="@Status"/>
              </xsl:attribute>
            </img>
          </td>
          
          <td>
            <xsl:value-of select="@LogItem"/>
          </td>
          
          <td class="textCentered">
            <xsl:value-of select="@ErrorCount"/>
          </td>
          
          <td class="textCentered">
            <xsl:value-of select="@WarningCount"/>
          </td>
          
          <td class="textCentered">
            <xsl:value-of select="@InfoCount"/>
          </td>
          
          <td class="textCentered">
            <xsl:value-of select="@PerfectCount"/>
          </td>
          
        </tr>
        
      </xsl:for-each>
    </table>
  </xsl:template>

  <!-- Document, matches "LogList" -->
  <xsl:template match="LogList">
    <html>
    
      <head>
        <meta content="en-us" http-equiv="Content-Language"/>
        <meta content="text/html; charset=utf-16" http-equiv="Content-Type"/>
        <link type="text/css" rel="stylesheet" href="Resources\LogList.css"/>
        <title>
          日誌統計報告
        </title>
      </head>
    
      <body>
        <h1>
          日誌統計報告
        </h1>
        <div id="content">
          <br/>
          <xsl:variable name="LogOverview">
            <xsl:apply-templates select="self::node()" mode="LogOverviewXML"/>
          </xsl:variable>
          <div id="overview">
            <xsl:apply-templates select="msxsl:node-set($LogOverview)/*" mode="LogOverview"/>
          </div>
        </div>
      </body>
      
    </html>
  </xsl:template>
  
</xsl:stylesheet>

 

這個文件要引用一個樣式表文件,建立「LogList.css」,也放到Resources目錄下,代碼以下:

/* 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: #E7E7E8;
    text-align: left;
    text-decoration: none;
    font-weight: normal;
    padding: 3px 6px 3px 6px;
}

table td
{
    vertical-align: top;
    padding: 3px 6px 5px 5px;
    margin: 0px;
    border: 1px solid #E7E7E8;
    background: #F7F7F8;
}

/* Center text, used in the over views cells that contain message level counts */ 
.textCentered
{
    text-align: center;
}

/* The message cells in message tables should take up all avaliable space */
.messageCell
{
    width: 100%;
}

/* Padding around the content after the h1 */ 
#content 
{
    padding: 0px 12px 12px 12px; 
}

(這個CSS文件刪減自上面所說的UpgradeReport.css)

如今再雙擊文件「LogList.xml」,就能夠在瀏覽器總看到下面的效果了:

附:本文中文件結構圖

C:\Users\Tsybius\Desktop\XLST_Test
|
|-Resources
| |
| |-0_PERFECT.png
| |-1_INFO.png
| |-2_WARNING.png
| |-3_ERROR.png
| |
| |-LogList.css
| |-LogList.xslt
|
|-LogList.xml

END

相關文章
相關標籤/搜索