Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XSLT - Edit XML


May 28, 2021 XSL T


Table of contents


XSLT - Edit XML

This section explains how to edit the data in an XML file.

The data stored in the XML file can be edited through an Internet browser.


Open, edit, and save XML

Now we'll show you how to open, edit, and save XML files stored on the server.

We'll use XSL to convert XML documents into an HTML form. T he value of the XML element is written to the HTML input field in the HTML form. T his HTML form is editable. After the edit is complete, the data is committed back to the server and the XML file is updated (this part is done by the ASP).


XML files and XSL files

First, look at the XML documentation that will be used ("tool.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<tool>
<field id="prodName">
<value>HAMMER HG2606</value>
</field>
<field id="prodNo">
<value>32456240</value>
</field>
<field id="price">
<value>$30.00</value>
</field>
</tool>

Next, look at the style sheet below ("tool.xsl"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<form method="post" action="edittool.html">
<h2>Tool Information (edit):</h2>
<table border="0">
<xsl:for-each select="tool/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="value" />
</xsl:attribute>
</input>
</td>
</tr>
</xsl:for-each>
</table>
<br />
<input type="submit" id="btn_sub" name="btn_sub" value="Submit" />
<input type="reset" id="btn_res" name="btn_res" value="Reset" />
</form>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

The XSL file above loops through the elements in the XML file and creates an input field for each XML "field" element. T he value of the "id" property of the XML "field" element is added to the "id" and "name" properties of each HTML input field. T he value of each XML "value" element is added to the "value" property for each HTML input field. As a result, you get an editable HTML form that contains values in the XML file.

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

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>Updated Tool Information:</h2>
<table border="1">
<xsl:for-each select="tool/field">
<tr>
<td><xsl:value-of select="@id" /></td>
<td><xsl:value-of select="value" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

THEP file

In the "tool.xsl" file above, the value of the action property 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 don't know how to write ANPs, learn our ASP tutorial.

Note: We are converting and updating the XML file on the server. T his is a cross-platform solution. Clients can only get HTML returned from the server - and HTML can run in any browser.

That's all about how to edit the data in the XML asking price, and you'll get to know the XML Editor in the next section.