3. Description of format conversion XSL file (Persons.xsl)
In the routine, XSL is used to format the XMl data and return it to the client in HTML. This process can also be carried out on the client side, but considering the compatibility issue, the routine adopts the method of formatting through ASP on the server side.
The contents of the XSL file are as follows:
<?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();">Add a new contact </a> </td> </tr> </table> <table align="center" cellpacing="1" cellpadding="2" bgcolor="#666600"> <tr bgcolor="#E5E5E5"> <td><xsl:text disable-output-escaping="yes">&</xsl:text>nbsp;</td> <td>Name</td> <td>English name</td> <td>Mobile phone</td> <td>Tel</td> <td>Email</td> <td>QQ</td> <td> Company where you are located</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">Modification information</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> |
The conversion on the server side is completed using a function. The formatting is successful, the HTML string is returned, the formatting is failed, and the error message is printed, as follows,
'************************************************ ' Description: Format the XML file using an XSL file. ' Author: gwd 2002-11-05 ' Parameters: strXmlFile -- Xml file, path + file name ' strXslFile -- Xsl file, path + file name ' Return: Success -- Formatted HTML string ' Failed -- Custom error message '************************************************ 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 'Catch errors in transformNode method 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"">Format XML file error!</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""> Error loading XSL file! </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""> Error loading XML file! </span>" End If Set objXsl = Nothing Set objXml = Nothing End Function |