Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XSLT – Editing XML

XSLT - Editing XML


Data stored in an XML file can be edited through an Internet browser.


Open, Edit, and Save XML

Now we will show you how to open, edit, and save an XML file stored on a server.

We will use XSL to transform the XML document into an HTML form. The values of the XML elements are written into HTML input fields in the form. This HTML form is editable. After editing is complete, the data is submitted back to the server and the XML file is updated (this part is done by ASP).


The XML File and the XSL File

First, look at the XML document to be used ("tool.xml"):

HAMMER HG2606

32456240

$30.00

View the XML file。

Next, look at the following stylesheet ("tool.xsl"):

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Tool Information (edit):


View the XSL file。

The XSL file above loops through the elements in the XML file and creates an input field for each XML "field" element. The value of the "id" attribute of each XML "field" element is added to the "id" and "name" attributes of each HTML input field. The value of each XML "value" element is added to the "value" attribute of each HTML input field. The result is an editable HTML form containing the values from the XML file.

Then we have a second stylesheet: "tool_updated.xsl". This XSL file is used to display the updated XML data. This stylesheet does not output an editable HTML form, but a static HTML table:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Updated Tool Information:

View the XSL file。


ASP file

In the "tool.xsl" file above, the value of the action attribute of the HTML form is "edittool.asp".

The "edittool.asp" page contains two functions: the loadFile() function loads and transforms the XML file, and the updateFile() function updates the XML file:

<%

function loadFile(xmlfile,xslfile)

Dim xmlDoc,xslDoc

'Load XML file

set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

xmlDoc.async = false

xmlDoc.load(xmlfile)

'Load XSL file

set xslDoc = Server.CreateObject("Microsoft.XMLDOM")

xslDoc.async = false

xslDoc.load(xslfile)

'Transform file

Response.Write(xmlDoc.transformNode(xslDoc))

end function

function updateFile(xmlfile)

Dim xmlDoc,rootEl,f

Dim i

'Load XML file

set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

xmlDoc.async = false

xmlDoc.load(xmlfile)

'Set the rootEl variable equal to the root element

Set rootEl = xmlDoc.documentElement

'Loop through the form collection

for i = 1 To Request.Form.Count

'Eliminate button elements in the form

if instr(1,Request.Form.Key(i),"btn_")=0 then

'The selectSingleNode method queries the XML file for a single node

'that matches a query. This query requests the value element that is

'the child of a field element that has an id attribute which matches

'the current key value in the Form Collection. When there is a match -

'set the text property equal to the value of the current field in the

'Form Collection.

set f = rootEl.selectSingleNode("field[@id='" & _

Request.Form.Key(i) & "']/value")

f.Text = Request.Form(i)

end if

next

'Save the modified XML file

xmlDoc.save xmlfile

'Release all object references

set xmlDoc=nothing

set rootEl=nothing

set f=nothing

'Load the modified XML file with a style sheet that

'allows the client to see the edited information

loadFile xmlfile,server.MapPath("tool_updated.xsl")

end function

'If the form has been submitted update the

'XML file and display result - if not,

'transform the XML file for editing

if Request.Form("btn_sub")="" then

loadFile server.MapPath("tool.xml"),server.MapPath("tool.xsl")

else

updateFile server.MapPath("tool.xml")

end if

%>

**Tip:** If you do not know how to write ASP, please study our ASP Tutorial。

**Note:** We are transforming and updating an XML file located on the server. This is a cross-platform solution. The client only receives the HTML returned from the server - and HTML can run in any browser.