Detailed description of code for online editing of XML documents using XSL and ASP

Source: Internet
Author: User
Tags xsl file
This article describes how to edit XML document data online through a detailed example. Due to Netscape's weak support for XML, data must be processed on the server to implement cross-platform data exchange. This article describes how to edit XML document data online through a detailed example. Due to Netscape's weak support for XML, data must be processed on the server to implement cross-platform data exchange. To edit the XML document, the first thing to do is to extract and display the data to visitors. XSL provides a good solution for us to display XML files. The following example uses the XSL style sheet to display the XML document for users to edit and then submit the edited data to the server, # php/php-tp-renewal.html "target =" _ blank "> update data on the server side. ASP (Active Server Pages) is used to complete our tasks.

First, load the XML document to be edited, and use Microsoft's Document Object Model (Microsoft XMLDOM Object) and XSL, XML files can be converted to HTML files displayed on the client on the server side. Next, let's take a look at what the XML and XSL files look like.

XML File: userdata. xml

 
 <用户资料>
  
   
    
Mencius
   
  
  
   
    
Male
   
  
  
   
    
China Network Technology Development Corporation Beijing branch
   
  
  
   
    
102 floor, Kerry Center, Beijing
   
  
  
   
    
1391139136 *
   
  
  
   
    
Amxh@21cn.com
   
  
 XSL file: userdata. xsl
 
 
  
   
  
 

The XSL file uses the XSL: for-each element to traverse the entire XML file, the "id" attribute of each "field" element in the XML file corresponds to the "id" and "name" in the text input box of the HTML form. In this way, the text input box of the HTML form displays the element values of the XML file. This file is responsible for the conversion of XML documents on the server, so that they can be displayed in various browsers.

The following is a key program that enables you to open and update XML documents, and determines whether to update the XML document based on whether the form is submitted or not. It contains two functions. loadXMLFile is used to load and convert the XML file to be displayed. updateXML is used to update the XML file.

The Edituserdata. asp program is as follows:

<% '-----------------------------------------------------------' Defines the loadXMLFile () function and receives two parameters: 'strxmlfile-XML file path and file name 'stringfilee-XSL file path and file name 'invalid Function loadXMLFile (strXMLFile, str1_file) 'Declare local variablesDim objXMLDim objXSL' instantiate the XMLDOM object, to load XML files. Set objXML = Server. CreateObject ("Microsoft. XMLDOM") 'to disable the asynchronous file loading mode. ObjXML. async = false' load the XML file! ObjXML. load (strXMLFile) 'instantiate the XMLDOM object to load the XSL file. Set objXSL = Server. CreateObject ("Microsoft. XMLDOM") 'to disable the asynchronous file loading mode. ObjXSL. async = false' load the XSL file! ObjXSL. load (str1_file) 'uses the XMLDOM transformNode method to apply the XSL style sheet to the XML document and then output it to the client. Response. Write (objXML. transformNode (objXSL) End Function 'signature' Function updateXML () receives a parameter: the path and file name of the strXMLFile-XML file. '------------------------------------------------------------------ Function updateXML (strXMLFile)' declares local variables. Dim objDomDim objRootDim objFieldDim x' instantiate the XMLDOM object. Set objDOM = Server. CreateObject ("Microsoft. XMLDOM") 'to disable the asynchronous file loading mode. ObjDOM. async = false' load the XML file. ObjDOM. load strXMLFile 'sets the root element. Set objRoot = objDom.doc umentElement 'traverses the FORM Set and writes the submitted data to the XML file. For x = 1 to Request. Form. Count 'check whether the submitted data contains buttons. If yes, ignore this data. If instr (1, Request. form. key (x), "btn") = 0 then' create the objField variable according to the XSL query mode, and map the elements of the form to the corresponding element [field_value] in the XML document. Set objField = objRoot. selectSingleNode ("field [@ id = '" & Request. form. key (x) & "']/field_value")' maps the data submitted by the form to the node values in the XML document. ObjField. Text = Request. Form (x) End IfNext 'saves the edited XML file. ObjDom. save strXMLFile 'releases all references to the object. Set objDom = NothingSet objRoot = NothingSet objField = Nothing 'call the loadXMLFile function and display the newly edited XML file to the client using the updateduserdata. xsl style sheet. LoadXMLFile strXMLFile, server. MapPath ("updateduserdata. xsl") End Function 'checks whether the form is successfully submitted, such as submitting and updating the XML file. Otherwise, the form is changed to the editing status. If Request. form ("btnSubmit") = "" ThenloadXMLFile server. mapPath ("userdata. xml "), server. mapPath ("userdata. xsl ") ElseupdateXML server. mapPath ("userdata. xml ") End If %>

After the form is submitted successfully, we use updateduserdata. xsl to display the data we just edited.

Updateduserdata. xsl is as follows:

 
 
  
   The updated user information is as follows:
   
   
 

The above is a detailed description of the code used to edit XML documents online using XSL and ASP. For more information, see other related articles in the first PHP community!

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.