A detailed example is presented to illustrate the method of editing XML document data online. Because Netscape has a weaker support for XML, data processing must be done on the server side to achieve Cross-platform data interchange. To edit an XML document, the first thing to do is to extract and display the data to the visitor, and XSL provides a good solution for us to display the XML file. The following example uses an XSL style sheet to display an XML document for editing by the user, and then submits the edited data to the server and updates the data on the server side. The ASP (Active Server Pages) is used here to accomplish our task.
First, load the XML document that we want to edit, and use the Microsoft Document Object model (Microsoft Xmldom object) and the Xsl,xml document to convert the content of the HTML file that can be displayed on the client server side. Let's look at the XML and XSL files we're using.
XML file: Userdata.xml
<?xml version= "1.0" encoding= "gb2312"?>
< user Information >
<field id= "Name" taborder= "1" >
<field_value> Mencius </field_value>
</field>
<field id= "Gender" taborder= "2" >
<field_value> male </field_value>
</field>
<field id= "unit name" taborder= "3" >
<field_value> China Network Technology Development company Beijing Branch </field_value>
</field>
<field id= "Detailed Address" taborder= "4" >
<field_value> Beijing Kerry Center 102 floor </field_value>
</field>
<field id= "Phone" taborder= "5" >
<field_value>1391139136*</field_value>
</field>
<field id= "e-mail" taborder= "6" >
<field_value>amxh@21cn.com</field_value>
</field>
</User profile >
XSL file: userdata.xsl
<?xml version= "1.0" encoding= "gb2312"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<body>
<form method= "POST" action= "edituserdata.asp" >
<table border= "1" cellpadding= "2" >
<xsl:for-each select= "user profile/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>
<br/>
<input type= "Submit" id= "Btnsubmit" name= "btnsubmit" value= "complete edit"/>
</form>
</body>
</xsl:template>
</xsl:stylesheet>
The XSL file uses the Xsl:for-each element to traverse the entire XML file, and the id attribute of each field element in the XML file corresponds to the ID and name of the text input box for the HTML form. In this way, the text entry box for an HTML form displays the element values of the XML file. This file is responsible for transforming the XML document on the server side so that it can be displayed on a variety of browsers. This article is from http://bianceng.cn (Getting started with programming)
The following are key programs that implement the ability to open and update XML documents and decide whether to update them based on the form's submission or not. It contains two functions, Loadxmlfile is responsible for loading and transforming the XML file to be displayed; The Updatexml function is responsible for updating the XML file.
The edituserdata.asp procedure is as follows:
<%
'-----------------------------------------------------------
' Define function Loadxmlfile () to receive two parameters:
' Strxmlfile-xml file's path and file name
' strxslfilee-xsl file's path and file name
'-----------------------------------------------------------
Function loadxmlfile (Strxmlfile, Strxslfile)
' Declare Local variables
Dim Objxml
Dim objxsl
' Instantiate the Xmldom object to load the XML file.
Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
' Turn off file asynchronous load mode.
Objxml.async = False
' Load XML file!
Objxml.load (Strxmlfile)
' Instantiate the Xmldom object to load the XSL file.
Set objxsl = Server.CreateObject ("Microsoft.XMLDOM")
' Turn off file asynchronous load mode.
Objxsl.async = False
' Load the XSL file!
Objxsl.load (Strxslfile)
' Using the Xmldom Transformnode method, apply the XSL stylesheet to the XML document and output it to the client.
Response.Write (Objxml.transformnode (objxsl))
End Function
'------------------------------------------------------------------
' function Updatexml () receives an argument: the path and file name of the Strxmlfile-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 file asynchronous load mode.
Objdom.async = False
' Loads an XML file.
Objdom.load Strxmlfile
' Set the root element.
Set Objroot = objdom.documentelement
' Iterate through the FORM collection and write the submitted data to an XML file.
For x = 1 to Request.Form.Count
' Check whether the submitted data contains buttons. If it is, ignore this data.
If InStr (1,request.form.key (x), "btn") = 0 Then
' According to the XSL query pattern, the Objfield variable is created, and the elements of the form are mapped to the corresponding elements in the XML document [Field_value].
Set objfield = Objroot.selectsinglenode ("field[@id = '" & Request.Form.Key (x) & "']/field_value")
' Match the data submitted by the form to the node values in the XML document.
Objfield.text = Request.Form (x)
End If
Next
' Save the edited XML file.
Objdom.save Strxmlfile
' Frees all references to objects.
Set objdom = Nothing
Set Objroot = Nothing
Set Objfield = Nothing
' Call the Loadxmlfile function to display the newly edited XML file to the client with the Updateduserdata.xsl style sheet.
Loadxmlfile Strxmlfile,server. MapPath ("updateduserdata.xsl")
End Function
' Check if the form was submitted successfully, such as Submit, update the XML file, or go to the edit state.
If Request.Form ("btnsubmit") = "" Then
Loadxmlfile server. MapPath ("Userdata.xml"), server. MapPath ("userdata.xsl")
Else
Updatexml server. MapPath ("Userdata.xml")
End If
%>
When the form is submitted successfully, we use updateduserdata.xsl to display the data we just edited.
Updateduserdata.xsl is as follows:
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body>
<table border="1" cellpadding="2">
<xsl:for-each select="user information/field">
<tr>
<td>
<xsl:value-of select="@id" />
</td>
<td>
<xsl:value-of select="field_value" />
</td>
</tr>
</xsl:for-each>
</table>
<form>
<input type="button" value="return" onclick="history.go(-1)" />
</form>
</body>
</xsl:template>
</xsl:stylesheet>
The above is just a simple example of XML Cross-platform application, combined with specific requirements, we can write more powerful programs to complete our more negative work. All programs are in Win98se+pws+ie5.5+netscape 4.75+netscape 6+MSXML3. Debugging through the DLL environment.