以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML源码及示例(仅原创和转载) 』  (http://bbs.xml.org.cn/list.asp?boardid=32)
----  [求助]ASP+XML怎样生成日志,急  (http://bbs.xml.org.cn/dispbbs.asp?boardid=32&rootid=&id=28751)


--  作者:meiyu_0707
--  发布时间:3/16/2006 11:40:00 AM

--  [求助]ASP+XML怎样生成日志,急

logInfo.asp
<%
function WriteSysLog(sys_userid,sys_username,operate)
  dim op_username
  if trim(sys_userid)="" and trim(sys_username)="" then   
    op_username="匿名"
  else
    op_username = sys_userid & "/" & sys_username
  end if
  
  '日志写入路径xmlPath
  dim xmlPath,xmlFile,RootNode,LogFile,fso,fff,mmm,xd,et,cnode,node2
  xmlPath="LogFile/"
  xmlFile=replace(cstr(ConvertDate(date())),"-","_") & ".xml"
  RootNode="syslog"                  '日志文件根节点名字
  LogFile=server.mappath(xmlPath & xmlFile)  '日志文件路径
  
  set fso=server.CreateObject("scripting.filesystemobject")
  '如果日志文件不存在,就创建一个,并写入头信息和根信息
  if not fso.FileExists(LogFile) then
    fso.CreateTextFile LogFile
    set fff=fso.GetFile(LogFile)
    set mmm=fff.openastextstream(2)
    mmm.write "<?xml version=""1.0"" encoding=""gb2312"" ?>" & vbcrlf & "<?xml-stylesheet type='text/xsl' href='logInfo.xsl'?>" & vbcrlf &  "<" & rootnode & "></" & rootnode & ">"
    set mmm=nothing
    set fff=nothing
  end if  
  set fso=nothing

  Set xd = Server.CreateObject("msxml2.domdocument")   
  xd.async = false   
  xd.load(LogFile)  
  if xd.parseError.errorcode<>0 then  
    WriteSysLog=1   '打开日志文件出错
    exit function
  end if  
  
  '创建新节点信息
  set et=xd.documentElement   
  set cnode=xd.createElement("log")
  et.appendchild(cnode)
  set node2=xd.createElement("username")
  node2.text=op_username
  cnode.appendchild(node2)
  set node2=xd.createElement("operate")
  node2.text=operate
  cnode.appendchild(node2)
  set node2=xd.createElement("userip")
  node2.text=Request.ServerVariables("Remote_Addr")
  cnode.appendchild(node2)
  set node2=xd.createElement("opdate")
  node2.text=cstr(now())
  cnode.appendchild(node2)
  xd.save LogFile   '写入日志文件

  set cnode=nothing
  set node2=nothing
  set xd=nothing
  
  writeSysLog=9  '说明正常写入了日志信息
end function   

'获得当前虚拟目录的名字
function getRoot()
  dim url
  url=Request.ServerVariables("URL")
  url=right(url,len(url)-1)
  getRoot= mid(url,1,instr(url,"/")-1)
end function

'将一个一位的数字前面加零
function FillZero(str)
  dim ttt
  ttt=str
  if len(str)=1 then
    ttt="0" & str
  end if
  FillZero=ttt
end function
'转化日期,将 一位补上零  2003-1-2  -->  2003-01-02
function ConvertDate(tDate)
  dim ttt
  ttt=tDate  
  if isdate(tDate) then
    ttt=year(tDate) & "-" & FillZero(month(tDate)) & "-" & FillZero(day(tDate))
  end if
  ConvertDate=ttt
end function
%>

logInfo.xsl
<?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" xmlns:user="http://www.cccar.com.cn/"
exclude-result-prefixes="msxsl user">

<!-- localized strings -->
<xsl:variable name='ColumnHeader_UserName'>用户</xsl:variable>
<xsl:variable name='ColumnHeader_Time'>时间</xsl:variable>
<xsl:variable name='ColumnHeader_Operate'>操作</xsl:variable>
<xsl:variable name='ColumnHeader_Address'>IP地址</xsl:variable>

<!-- variables -->
<xsl:variable name='TableStyle'>background-color:#DAE6D8;font-family:Simsun, Verdana; font-size:75%; text-align:left; vertical-align:top</xsl:variable>
<xsl:variable name='HeaderStyle'>background:a0b0a8;color:#000000;border-bottom:1 solid black;border-top:1 solid black</xsl:variable>
<msxsl:script language="javascript" implements-prefix="user">
   function xmlDateTime(nodelist) {
           return Date.parse(nodelist.replace(/-/g,"/"));
        }
</msxsl:script>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="syslog">

    <html>
    <head>
        <title>
            日志查看
        </title>
    </head>

    <body style='margin:10;background-color:#DAE6D8'>
        <div align="center">
        <table style="{$TableStyle}" width="100%" align="center" cellspacing='0'>
          
            <thead>
                <tr height="23">
                    
                    <th width="15%" style="{$HeaderStyle}">
                        <xsl:value-of select="$ColumnHeader_UserName"/>
                    </th>
                    <th width="20%" style="{$HeaderStyle}">
                        <xsl:value-of select="$ColumnHeader_Time"/>
                    </th>
                    <th width="50%" style="{$HeaderStyle}">
                        <xsl:value-of select="$ColumnHeader_Operate"/>
                    </th>
                    <th width="15%" style="{$HeaderStyle}">
                        <xsl:value-of select="$ColumnHeader_Address"/>
                    </th>
                </tr>
            </thead>
            
            <tbody style='vertical-align:top'>
                 <tr ><td colspan="4" height="5"></td></tr>  
                 <xsl:for-each select="log">
                 <xsl:sort order='ascending' select="user:xmlDateTime(string(opdate))" data-type="number"/>
                     
                   <tr height="23">
                    
                     <td valign="bottom"><xsl:value-of select="username"/></td>
                     <td valign="bottom" ><xsl:value-of select="opdate"/></td>
                     <td valign="bottom" ><xsl:value-of select="operate"/></td>
                     <td valign="bottom" ><xsl:value-of select="userip"/></td>  
                  </tr>
                  <tr bgcolor="#999999"><td colspan="4" height="1"></td></tr>  
                </xsl:for-each>
                
                <tr><td colspan="4" align="right">合计:<xsl:value-of select="count(log)"/> 条 </td></tr>    
            </tbody>
        </table>
        </div>
    </body>
    </html>

</xsl:template>
</xsl:stylesheet>

我生成的日志显示:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type='text/xsl' href='logInfo.xsl'?>
<syslog><log><username>1/bf</username><operate>修改</operate><userip>192.168.0.232</userip><opdate>2006-3-16 9:34:49</opdate></log><log><username>1/bf</username><operate>修改</operate><userip>192.168.0.232</userip><opdate>2006-3-16 10:44:56</opdate></log><log><username>1/bf</username><operate>修改</operate><userip>192.168.0.232</userip><opdate>2006-3-16 10:45:13</opdate></log></syslog>

怎样生成显示:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type='text/xsl' href='logInfo.xsl'?>
<syslog>    
  <log>   
    <username>1/bf</username>   
    <operate>修改</operate>
    <userip>192.168.0.232</userip>
    <opdate>2006-3-17 9:34:27</opdate>
  </log>
  <log>   
    <username>1/bf</username>   
    <operate>修改</operate>
    <userip>192.168.0.232</userip>
    <opdate>2006-3-17 9:34:27</opdate>
  </log>
  <log>   
    <username>1/bf</username>   
    <operate>修改</operate>
    <userip>192.168.0.232</userip>
    <opdate>2006-3-17 9:34:27</opdate>
  </log>
</syslog>
这样看起来直观点
哪位指点下,我很着急,谢谢了,我刚学XML


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
2,015.625ms