執行個體演練ASP+XML編程(2)

來源:互聯網
上載者:User
三、格式轉換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 width="600" border="0" align="center">
 <tr>
  <td align="right"><a href="javascript:add();" title="添加新連絡人">添加新連絡人</a>  </td>
 </tr>
</table>

<table align="center" width="680" cellspacing="1" cellpadding="2" border="0" bgcolor="#666600">
 <tr class="title" bgcolor="#E5E5E5">
  <td width="25"><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 = "<span class=""alert"">格式化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 = "<span class=""alert"">裝載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 = "<span class=""alert"">裝載XML檔案錯誤!</span>"
 End If

 Set objXsl = Nothing
 Set objXml = Nothing
End Function



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.