>+ 实例演练ASP+XML编程(二)

类别:.NET开发 点击:0 评论:0 推荐:
三、格式转换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

本文地址:http://com.8s8s.com/it/it41054.htm