Edit an XML document with xsl.asp

Source: Internet
Author: User
Tags format contains count html form variable xsl xsl file xsl stylesheet
Brief introduction

This article is a "Save to HTML table data to XML" sister article. If you haven't read the above, I suggest you have a look first. This article is built on the basis of above. As for the above example, the reader is constantly giving a positive response, and again, many people want to know how to edit XML data. So, I wrote down this article.

Using XSL state: Open an XML file, determine that it will be edited, routed to an HTML form, and eventually routed to the browser. The value of this XML element will be set to the value of the HTML input field. After these necessary edits, you can submit the processed information to the server and the XML file is also updated.

The first step is the file you will edit and appear in the browser as an HTML form. In the following example, the change of XML on the server was skipped, and the XML file could be transformed into XSL files under the xmldom target of Microsoft. We can also use this technique to transform XML files here.

XML File:contact.xml:
<?xml version= "1.0"? > >
<contact>
<field id= "FirstName" taborder= "1"
<field_value> Michael </field_value>
</field>
<field id= "LastName" taborder= "2"
<field_value> Qualls </field_value>
</field>
<field id= "Address1" taborder= "3"
<field_value> Haverbrook East </field_value>
</field>
<field id= "Address2" taborder= "4"
<field_value> Oklahoma City, OK 73114 </field_value>
</field>
<field id= "Phone" taborder= "5"
<field_value> 4055551234 </field_value>
</field>
<field id= "Email" taborder= "6"
<field_value> mqualls@vertiscope.com </field_value>
</field>
</contact>
The XML file used in this article is the same as the example in the article "Saving HTML tables to XML". So you can see the connection in a more intuitive light.

XSL File:contact.xsl:

<?xml version= "1.0"? > >
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl"
<xsl:template match= "/" >
<body>
<form method= "POST" action= "editcontact.asp"
<table border= "1" cellpadding= "2"
<xsl:for-each select= "Contact/field"
<tr>
<td>
<xsl:value-of select= "@id"/>
</td>
<td>
<input type= "text" >
<xsl:attribute name= "id" >
<xsl:value-of select= "@id"/>
</xsl:attribute>
<xsl:attribute name= "Name" >
<xsl:value-of select= "@id"/>
</xsl:attribute>
<xsl:attribute name= "value" >
<xsl:value-of select= "Field_value"/>
</xsl:attribute>
</input>
</td>
</tr>
</xsl:for-each>
</table>
<input type= "Submit" id= "Btnsubmit" name= "btnsubmit" value= "submit"/>
</form>
</body>
</xsl:template>
</xsl:stylesheet>
This XSL file uses the For-each XSL element to iterate over the elements of the XSL file.

Starting with this root element, the ID of each XML "domain" element is written as the "id" and "NAME" of the HTML text field.

Similarly, the value of the "field value/field_value" element in an XML file is also written as a value/value in each HTML text field. The final result is that the HTML format contains the values that will be edited from the XML file.

I put "ID" from the "domain" element in the XML file and placed it in the HTML text field in the XSL file to avoid confusion and to promote the continuity of the naming. In this way, friends who are not familiar with coding knowledge can also tell which XML domain to match with which HTML domain.

By using the two files above, we are well prepared to begin editing the XML file. The XSL file will transfer the XML file so that it can be displayed on the browser. We can do this on the terminal, but not the best solution. With ASP, we can do this transfer work on the server. In the same way, we can edit XML files on the server.
Example: Editing XML by using xsl,asp

Editing contact.asp is a more common phenomenon. Here are two features that play a major role in editing ASP pages. The first is the Loadxmlfile function, which load and transfer the XML file to display it; the second is the Updatexml feature, which is suitable for editing the XML file country.

ASP File:EditContact.asp:

<%
'-----------------------------------------------------------
The ' loadxmlfile ' function accepts two parameters.
' Strxmlfile-xml the path name and filename of the file.
' Strxslfilee-xsl the path name and filename of the file.
'-----------------------------------------------------------
Function loadxmlfile (Strxmlfile, Strxslfile)
' Local variable

Dim objxml Dim Objxsl
' Initializes the Xmldom object.

Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
' Close the files that are loaded synchronously.
Objxml.async = False

' Load XML file.

Objxml.load (Strxmlfile)
' Initializes the Xmldom object used to load the XSL file.
Set objxsl = Server.CreateObject ("Microsoft.XMLDOM")
' Turn off asyncronous file loading.
Objxsl.async = False ' Load the XSL file.
Objxsl.load (Strxslfile)
' Use the ' transformnode ' for the ' xmldom ' to apply the
' XSL stylesheet to the XML document. Then the output is
' Written to the client.
Response.Write (Objxml.transformnode (objxsl))
End Function
'-----------------------------------------------------------
' The ' Updatexml ' Function accepts one parameter.
' Strxmlfile-the path and file name of the XML file.
'-----------------------------------------------------------

Function Updatexml (Strxmlfile)
' Declare local variables.
Dim Objdom
Dim Objroot
Dim Objfield
Dim x
' Instantiate the XMLDOM Object.
Set objdom = Server.CreateObject ("Microsoft.XMLDOM")
' Turn off asyncronous file loading.
Objdom.async = False
' Load the XML file.
Objdom.load Strxmlfile
' Set the objroot variable equal to the ' root element of the
' XML file by calling the ' documentelement '
' Objdom (xmldom) object.
Set Objroot = objdom.documentelement
' Iterate through the Form Collection and write the
' submitted values to the XML file.
For x = 1 to Request.Form.Count
' Check-if ' btn ' is in the submitted value, if so,
' It is a button and should to be ignored.
If InStr (1,request.form.key (x), "btn") = 0 Then
' Set objfield variable equal to a Field_value element by
' Calling the selectSingleNode method of the Objroot
' (documentelement) object. The selectSingleNode method
' accepts a string parameter for querying the XML document.
' In this case, the value of the ' key ' of
' The Form Collection is used to find the appropriate
' Field_value element (more on this later).
    
Set objfield = Objroot.selectsinglenode ("field[@id = '" & _ Request.Form.Key (x) & "']/field_value")
' Set ' The Text property of the Objfield (Field_value)
' Element equal to ' value of the '
Objfield.text = Request.Form (x)
End If
Next
' After the XML file has been edited, are must be saved.
Objdom.save Strxmlfile
' Release all of your object references.
Set objdom = Nothing
Set Objroot = Nothing
Set Objfield = Nothing
' Call the Loadxmlfile method, passing in the newly edited
' XML file and the updatedcontact.xsl style sheet. This would
' Allow the client to the edited information. More on the
' Updatedcontact.xsl file later.
Loadxmlfile Strxmlfile,
Server. MapPath ("updatedcontact.xsl")
End Function
' Test to ' if the form has been submitted. If it has,
' Update the XML file. If not, transform the XML file for
' Editing.
If Request.Form ("btnsubmit") = "" Then
Loadxmlfile server. MapPath ("Contact.xml"), _ Server. MapPath ("contact.xsl")
Else
Updatexml server. MapPath ("Contact.xml")
End If
%>

As you can see, the ASP file handles the process of updating the entire XML file. If the form has been submitted, the XML file is opened and updated. If the form is not submitted, the XML file is sent by contact.xsl to the HTML format so that the user can edit it himself. Please refer to the following examples:

For x = 1 to Request.Form.Count
If InStr (1,request.form.key (x), "btn") = 0 Then
Set objfield = Objroot.selectsinglenode ("field[@id = '" & _ Request.Form.Key (x) & "']/field_value")
Objfield.text = Request.Form (x)
End If
Next
 
The code above is the code that updates the XML file. selectSingleNode method is the key.


In the above example, the question is "field[@id = '" & Request.form.key (x) & "]/field_value". The question is: the field_value element required to be a subdomain element contains an "id" that matches the key value in the existing form collection. Once you have the appropriate node, you can update the text properties to match the values in form collection.


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.