三、格式轉換XSL文件說明(Persons.xsl)
例程中使用XSL對XMl數據進行格式化,並以HTML的形式返回到客戶端。這個過程也可以放在客戶端進行,但考慮到兼容性的問題,例程中採用了在服務器端通過ASP操縱DOM進行格式化的方法。
XSL文件的內容如下,
<?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/Persons"> <script language="javascript"> function add() { window.open("add.asp", "add", "width=300,height=320,resize=no"); } function edit(intId) { window.open("edit.asp?id="+intId, "edit", "width=300,height=320,resize=no"); } </script> <table align="center"> <tr> <td align="right"><a href="javascript:add();">添加新聯繫人</a> </td> </tr> </table> <table align="center" cellspacing="1" cellpadding="2" bgcolor="#666600"> <tr bgcolor="#E5E5E5"> <td><xsl:text disable-output-escaping="yes">&</xsl:text>nbsp;</td> <td>姓名</td> <td>英文名</td> <td>手機</td> <td>電話</td> <td>Email</td> <td>QQ</td> <td>所在公司</td> </tr> <xsl:for-each select="Person"> <TR BGCOLOR="#FFFFFF"> <TD ALIGN="right"><xsl:value-of select="position()"/></TD> <TD STYLE="color:#990000"><A><xsl:attribute name="HREF">javascript:edit('<xsl:value-of select="position()"/>');</xsl :attribute><xsl:attribute name="title">修改信息</xsl:attribute><xsl:value-of select="Name"/></A></TD> <TD><xsl:value-of select="Nick"/></TD> <TD><xsl:value-of select="Mobile"/></TD> <TD><xsl:value-of select="Tel"/></TD> <TD><A><xsl:attribute name="HREF">mailto:<xsl:value-of select="Email"/></xsl:attribute><xsl:value-of select="Email"/> </A></TD> <TD><xsl:value-of select="QQ"/></TD> <TD><xsl:value-of select="Company"/></TD> </TR> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> |
在服務器端的轉換使用一個函數來完成,格式化成功,返回HTML字符串,格式化失敗,打印出錯誤信息,如下,
'******************************************* ' 說明:使用XSL文件格式化XML文件。 ' 作者:gwd 2002-11-05 ' 參數:strXmlFile -- Xml文件,路徑+文件名 ' strXslFile -- Xsl文件,路徑+文件名 ' 返回:成功-- 格式化後的HTML字符串 ' 失敗-- 自定義的錯誤信息 '******************************************* Function FormatXml(strXmlFile, strXslFile) Dim objXml, objXsl strXmlFile = Server.MapPath(strXmlFile) strXslFile = Server.MapPath(strXslFile) Set objXml = Server.CreateObject("MSXML2.DOMDocument") Set objXsl = Server.CreateObject("MSXML2.DOMDocument") objXML.Async = False If objXml.Load(strXmlFile) Then objXsl.Async = False objXsl.ValidateonParse = False If objXsl.Load(strXslFile) Then On Error Resume Next ' 捕獲transformNode方法的錯誤 FormatXml = objXml.transformNode(objXsl) If objXsl.parseError.errorCode <> 0 Then Response.Write "<br><hr>" Response.Write "Error Code: " & objXsl.parseError.errorCode Response.Write "<br>Error Reason: " & objXsl.parseError.reason Response.Write "<br>Error Line: " & objXsl.parseError.line FormatXml = "<spanalert"">格式化XML文件錯誤!</span>" End If Else Response.Write "<br><hr>" Response.Write "Error Code: " & objXsl.parseError.errorCode Response.Write "<br>Error Reason: " & objXsl.parseError.reason Response.Write "<br>Error Line: " & objXsl.parseError.line FormatXml = "<spanalert"">裝載XSL文件錯誤!</span>" End If Else Response.Write "<br><hr>" Response.Write "Error Code: " & objXml.parseError.errorCode Response.Write "<br>Error Reason: " & objXml.parseError.reason Response.Write "<br>Error Line: " & objXml.parseError.line FormatXml = "<spanalert"">裝載XML文件錯誤!</span>" End If Set objXsl = Nothing Set objXml = Nothing End Function |