Asp+xml Instance Walkthrough Programming code 1th/3 Page _ Application Tips

Source: Internet
Author: User
Tags html form naming convention xsl xsl file
Instance Walkthrough Asp+xml Programming

This article is an example to explain the article. As an ordinary programmer, I am well aware that an excellent routine is very helpful to those who are learning to program. The routines used in this article are a contact information management program that I also write to make it easy for me to connect with my friends. But though small, spite, believe to be learning Asp+xml programming friends, or have a certain reference value.

This example allows readers to learn how to manipulate XML files in an ASP (Active Server Page) and perform various processing of data, including building, modifying, deleting, and saving XML nodes, and so on. The techniques involved include asp,vbscript,dom,xml and XSL.

This article does not carry on the in-depth theory introduction to the use technology, therefore, the reader needs to have certain relevant knowledge, especially to ASP, XML and Dom should have certain understanding. Read through this article, and refer to the source code, I believe that the reader must be proficient in mastering XML programming.
I. Description of the procedure

The routines are based on B/s structure, use XML files to store contact information, and then use the DOM to perform various operations on the contact information in the XML file using a class written in VBScript.

Routines provide code that uses a uniform naming convention, which includes: a three-letter abbreviation for variable types, such as numeric type--int, String type--str, Object--obj, and so on, although in Asp/vbscript, the data type is not distinguished, but with obvious data type descriptions, It also makes sense to write and maintain programs, using meaningful variable names, such as XmlDocument objects, defined as objXMLDoc, and so on, and so on, as well, to better write and maintain programs.

This program can be divided into two parts: Background data processing and front interface performance.

Program backstage, you write a class using VBScript, which is a new feature provided in the VBScript5.0 version. Although the concept of class here and the real object-oriented, but the rational use of class in the ASP, or to a certain extent to improve the operation of the program efficiency and maintainability.

Foreground performance, using XSL to format the data in the XML file, and then outputting it to the client in HTML, fully embodies the flexibility and customization that XML technology brings. The format of the process is placed on the server side, using an ASP program to complete, so that the client is the formatted HTML information to avoid the emergence of compatibility issues.

Of course, the program does not have a very strict inspection of specific operational details, such as the check of the contact information required, but the program provides complete sample code for the relevant part of using DOM to manipulate XML in ASP.
Ii. XML Document Description (Persons.xml)

The XML file structure used in routines is very simple and does not define a schema or DTD associated with it, because this is not necessary for this program. Of course, if the reader is willing to define one of its own, it will not affect the operation of the program.

The program's data structure is defined as the persons collection, which contains multiple person objects, each with name name, English name Nick, mobile phone, telephone tel, email email, Tencent QQ, and Company properties. The above definition corresponds to an XML file that is, persons is the root node, and person is the child of the persons, Name, Nick, Mobile, Tel, Email, QQ, and company are child nodes of person.

In this way, the contents of the XML file we get are as follows:
<?xml version= "1.0" encoding= "gb2312"?
<Persons>
<Person>
<Name> Small East </Name>
<Nick> GWd </Nick>
<Mobile> 139XXXXXXXX </Mobile>
<Tel> XXXXXXXX </Tel>
<Email> gwd@chinaren.com </Email>
<QQ> 7066015 </QQ>
<Company> XXX </Company>
<Person>
</Person>
Readers need to pay attention to <?xml version= "1.0" encoding= "gb2312" the line, the XML does not support Chinese, by setting the Encoding property, you can make the XML correctly display Chinese. Readers can access this file in IE5.0 and above browsers, and it will display the data in a tree-shaped structure.
Turn from: Dynamic Network production guide www.knowsky.com

Turn from: Dynamic Network production guide www.knowsky.com
Iii. format Conversion XSL file description (persons.xsl)

The routine uses XSL to format the XML data and return it to the client in HTML form. This process can also be done on the client side, but given the compatibility problem, the routine uses the ASP to manipulate the DOM on the server side to format the method.

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 width= "border=" "0" align= "center"
<tr>
<TD align= "Right" ><a href= Javascript:add (); "title= Add New Contact" > add new contact </a> </td>
</tr>
</table>

<table align= "center" width= "680" cellspacing= "1" cellpadding= "2" border= "0" bgcolor= "#666600"
<TR class= "title" Bgcolor= "#E5E5E5"
<TD width= "><xsl:text" disable-output-escaping= "yes" >& </xsl:text> nbsp; </td>
<td> name </td>
<td> English name </td>
<td> Mobile </td>
<td> Phone </td>
<td> Email </td>
<td> QQ </td>
<td> your company </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" Modify 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 uses a function to complete, format successfully, return HTML string, format failed, print out the error message, such as the following,
'*******************************************
' Description: Format the XML file with the XSL file.
' Author: gwd 2002-11-05
' Parameters: Strxmlfile--XML file, path + filename
' Strxslfile--xsl file, path + filename
' Return: Success--formatted HTML string
' Failure--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 ' captures Transformnode method errors
Formatxml = Objxml.transformnode (objxsl)
If objXsl.parseError.errorCode <> 0 Then
Response.Write "<br> 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" "> format XML file Error!" </span> the
End If
Else
Response.Write "<br> 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" "> loading XSL file error!" </span> the
End If
Else
Response.Write "<br> 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" "> loading XML file Error!" </span> the
End If
Set objxsl = Nothing
Set Objxml = Nothing
End Function
Current 1/3 page 123 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.