This article explains the method of online editing of XML document data through a detailed example. Since Netscape's support for XML is relatively weak, to achieve cross-platform data exchange, data processing must be performed on the server side. To edit an XML document, the first thing to do is how to extract and display the data to visitors. XSL provides a good solution for us to display XML files. The following example uses an XSL stylesheet to display an XML document for users to edit, and then submits the edited data to the server, where the data is updated on the server side. ASP (Active Server Pages) is used here to complete our tasks.
First, load the XML document we want to edit. Using Microsoft's Document Object Model (Microsoft XMLDOM Object) and XSL, the XML document can be converted on the server side into HTML file content that can be displayed on the client side. Let's first take a look at what the XML and XSL files we use look like.
XML file: UserData.xml
<?xml version="1.0" encoding="gb2312"?>
<!-- edited with XMLSPY v2004 rel. 4 U ( http://www.xmlspy.com ) by Dicky (Apple'S Eden) -->
<UserInfo>
<Field ID="Name" TabOrder="1">
<FieldValue>Dicky</FieldValue>
</Field>
<Field ID="Sex" TabOrder="2">
<FieldValue>Male</FieldValue>
</Field>
<Field ID="UnitName" TabOrder="3">
<FieldValue>ShangHai Ateam Company</FieldValue>
</Field>
<Field ID="Address" TabOrder="4">
<FieldValue>ShangHai,ZhongShan West Road 1800#,Room 26A</FieldValue>
</Field>
<Field ID="Tel" TabOrder="5">
<FieldValue>13800138000</FieldValue>
</Field>
<Field ID="EMail" TabOrder="6">
<FieldValue>[email protected]</FieldValue>
</Field>
</UserInfo>
XSL file: UserData.xsl
<?xml version="1.0" encoding="gb2312"?>
<!-- edited with XMLSPY v2004 rel. 4 U ( http://www.xmlspy.com ) by Dicky (Apple'S Eden) -->
<xsl:stylesheet xmlns:xsl=" http://www.w3.org/TR/WD-xsl ">
<xsl:template match="/">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<body>
<form method="post" action="EditUserData.asp">
<h1>Edit UserInfo:</h1>
<table border="1" cellpadding="2">
<xsl:for-each select="UserInfo/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="FieldValue"/></xsl:attribute>
</input>
</td>
</tr>
</xsl:for-each>
</table>
<br/>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Edit"/>
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
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 and the "id" and "id" of the text input box of the HTML form name" corresponds to. In this way, the text input box of the HTML form displays the element value of the XML file. This file is responsible for the server-side conversion of XML documents so that they can be displayed on various browsers.
The following is the key program, which implements the function of opening and updating XML documents, and decides whether to update based on whether the form is submitted or not. It contains two functions, loadXMLFile is responsible for loading and converting the XML file to be displayed; the updateXML function is responsible for updating the XML file.
The EditUserData.asp program is as follows:
<%
'------------------------------------------------ ----------
'Define the function LoadXMLFile(), which receives two parameters:
'strXMLFile - The path and file name of the XML file 'strXSLFilee - The path and file name of the XSL file'-------------------------------- ----------------------------------
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 loading 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 loading mode.
objXSL.Async = False
'Load XSL file!
objXSL.Load(strXSLFile)
'Use the transformNode method of XMLDOM to apply the XSL style sheet to the XML document and then output it to the client.
Response.Write(objXML.TransFormNode(objXSL))
End Function
'------------------------------------------------ ------------------
'The function updateXML() receives 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 file asynchronous loading mode.
objDom.Async = False
'Load XML file.
objDom.Load strXMLFile
'Set the root element.
Set objRoot = objDom.DocumentElement
'Traverse the FORM collection and write the submitted data to the XML file.
For x = 1 To Request.Form.Count
'Check if the submitted data contains a button. If so, ignore this data.
If Instr(1,Request.Form.Key(x),"btn") = 0 Then
'According to the XSL query mode, establish the objField variable and map the elements of the form to the corresponding elements [field_value] in the XML document.
Set objField = objRoot.SelectSingleNode("Field[@ID='" & Request.Form.Key(x) & "']/FieldValue")
'Correlate the data submitted by the form with the node values in the XML document.
objField.Text = Request.Form(x)
End If
Next
'Save the edited XML file.
objDom.Save strXMLFile
'Release all references to the object.
Set objDom = Nothing
Set objRoot = Nothing
Set objField = Nothing
'Call the LoadXMLFile function to display the newly edited XML file to the client using the UpdatedUserData.xsl style sheet.
LoadXMLFile strXMLFile,Server.MapPath("UpdatedUserData.xsl")
End Function
'Check whether the form is submitted successfully, if submitted, update the XML file; otherwise, go to the editing 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"?>
<!-- edited with XMLSPY v2004 rel. 4 U ( http://www.xmlspy.com ) by Dicky (Apple'S Eden) -->
<xsl:stylesheet xmlns:xsl=" http://www.w3.org/TR/WD-xsl ">
<xsl:template match="/">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<body>
<h1>Updated UserInfo:</h1>
<table border="1" cellpadding="2">
<xsl:for-each select="UserInfo/Field">
<tr>
<td>
<xsl:value-of select="@ID"/>
</td>
<td>
<xsl:value-of select="FieldValue"/>
</td>
</tr>
</xsl:for-each>
</table>
<form>
<input type="button" value="Go Back" onclick="history.go(-1)"/>
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The above is just a simple example of XML cross-platform application. Based on specific needs, we can write more powerful programs to complete our more complex work. All programs passed debugging under Windows 2003 En+IIS 6.0+IE6.0+MSXML3.Dll environment.