Online editing of XML documents using XSL and ASP
Author:Eve Cole
Update Time:2009-06-23 16:38:26
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"?>
<User profile>
<field id="Name" taborder="1">
<field_value>Mencius</field_value>
</field>
<field id="gender" taborder="2">
<field_value>Male</field_value>
</field>
<field id="Organization name" taborder="3">
<field_value>China Network Technology Development Corporation Beijing Branch</field_value>
</field>
<field id="Detailed address" taborder="4">
<field_value>102nd Floor, Kerry Center, Beijing</field_value>
</field>
<field id="Telephone" taborder="5">
<field_value>1391139136*</field_value>
</field>
<field id="email" taborder="6">
<field_value>[email protected]</field_value>
</field>
</user profile>
'www.knowsky.com
XSL file: userdata.xsl
<?xml version="1.0" encoding="gb2312" ?>
<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>User profile editing:</h1>
<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 editing" />
</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 corresponds to the "id" and "name" of the text input box of the HTML form. 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
''www.knowsky.com
''------------------------------------------------ ----------
Function loadXMLFile(strXMLFile, strXSLFile)
''Declare local variables
Dim objXML
Dim objXSL
''Instantiate an XMLDOM object to load XML files.
set objXML = Server.CreateObject("Microsoft.XMLDOM")
''Turn off asynchronous file 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 asynchronous file 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 asynchronous file loading mode.
objDOM.async = false
''Load the 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) & "'']/field_value")
'' Corresponds 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
''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" ?>
<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>The updated user information is as follows:</h1>
<table border="1" cellpadding="2">
<xsl:for-each select="User profile/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>
</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 were debugged and passed in the WIN98SE+PWS+IE5.5+Netscape 4.75+Netscape 6+MSXML3.DLL environment.