In this paper, a method of editing XML document data online is described herein. Since NetScape is relatively weak supporting XML, the processing of data must be performed on the server side. To edit the XML document, what you want to do is how to extract these data and display to visitors, XSL provides us with a good solution to our XML file. The following example is to display the XML document with the XSL style to the user to edit, and then submit the edited data to the server and update the data on the server. Here is an ASP (Active Server Pages) to complete our task.
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 to the server side to the HTML file content that can be displayed on the client. Let's take a look at what we use the XML and XSL files.
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
>
Applebbs@gmail.com
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" of the text input box for the HTML form correspond to "Name". Thus, the text input box of the HTML form shows the element value of the XML file. This file is responsible for the transformation of the XML document in the server side so that it can be displayed on various browsers. The following is a key program that implements the function of opening and updating the XML document, and decides whether it is updated according to the submission of the form. It contains two functions, LoadXMLFile is responsible for loading and converting the XML files to be displayed; UpdateXML functions are responsible for updating XML files. The EditUserData.asp program is as follows:
<
%
'
-------------------------------------------------- ---------
'
Define function loadXmlFile (), receive two parameters:
'
Strxmlfile - path and file name of XML file
'
STRXSLFILEE - XSL file 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 the 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 the file asynchronous load mode.
Objxsl.async
=
False
'
Load XSL file!
Objxsl.Load (strxslfile)
'
Use XMLDOM's TransformNode method to apply the XSL style sheet to the XML document, and then output to the client.
Response.write (objxml.transformnode (objxsl))
END FUNCTION
'
-------------------------------------------------- ----------------
'
Function updateXML () receives a parameter: strXmlfile - the path and file name of the XML file.
'
-------------------------------------------------- ----------------
FUNCTION
Updatexml (strxmlfile)
'
Declare a local variable.
DIM
Objdom
DIM
Objroot
DIM
Objfield
DIM
x
'
Instantiate the XMLDOM object.
Set
Objdom
=
Server.
CreateObject
(
"
Microsoft.xmldom
"
)
'
Turn off the file asynchronous load mode.
ObjDom.async
=
False
'
Load an XML file.
Objdom.Load Strxmlfile
'
Set the root element.
Set
Objroot
=
Objdom.documentelement
'
Traverse the Form collection and write submitted data into 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
'
Follow the XSL query mode, establish the Objfield variable, corresponding to the elements of the form to the corresponding elements in the XML document [field_Value].
Set
Objfield
=
ObjRoot.selectsinglenode (
"
Field [@ id = '
"
&
Request.form.Key (x)
&
"
'] / FieldValue
"
)
'
Put the data submitted by the form and the node value in the XML document.
Objfield.text
=
Request.form (x)
End
IF
NEXT
'
Save the edited XML file.
Objdom.save strxmlfile
'
Release all references to objects.
Set
Objdom
=
Nothing
Set
Objroot
=
Nothing
Set
Objfield
=
Nothing
'
Call the loadXmlFile function, display the newly edited XML file with the UpdatedUserData.xsl style list to the client.
Loadxmlfile strXmlfile, Server.mappath (
"
UpdatedUserData.xsl
"
)
END FUNCTION
'
Check if the form is successfully submitted, such as submitting, updating XML files; 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, 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 applications, combining specific needs, we can write more powerful programs to complete our more negative work. All programs are debugged in the Windows 2003 En IIS 6.0 IE6.0 MSXML3.DLL environment.