Data stored in XML files can be edited from an Internet browser.
储存在XML文件中的数据可以在英特网浏览器上进行编辑。
Open, Edit and Save XML
打开、编辑和保存XML的方法
Now, we will show how to open, edit, and save an XML file that is stored on the server.
现在我们将演示一下如何打开、编辑和保存一份储存在服务器上的XML文件。
We will use XSL to transform the XML document into an HTML form. The values of the XML elements will be written to HTML input fields in an HTML form. The HTML form is editable. After editing the data, the data is going to be submitted back to the server and the XML file will be updated (this part is done with ASP).
我们将用XSL来把XML 文档转换成HTML形式。XML元素值将以HTML的格式写入HTML输入域(input field)。HTML的格式是可以被编辑的。数据被编辑之后将被重新传回服务器,XML也将被升级(这份HTML元素值将被写到HTML表格中的HTML输入栏)。
The XML File and the XSL File
XML文件和XSL文件
First, look at the XML document that will be used ("tool.xml"):
首先,来看一下要用到的 XML文件("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> |
Then, take a look at the following style sheet ("tool.xsl"):
让我们看一下下面的样式表("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.asp"> <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 one input field for each XML "field" element. The value of the XML "field" element's "id" attribute is added to both 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 that contains the values from the XML file.
上面的XSL文件对XML文件里的元素进行循环,并为每一个XML域(field)创建了输入域(input field)。XML“域(field)”元素的ID属性值被添加到每个HTML输入域(input field)的“ID”和“名字”属性中。每个XML“值(value)”元素的值要被添加到每个HTML输入域的“值”属性里。结果是一个可编辑的HTML表格包含了来自XML文件中的值。
Then, we have a second style sheet: "tool_updated.xsl". This is the XSL file that will be used to display the updated XML data. This style sheet will not result in an editable HTML form, but a static HTML table:
接下来,我们开始第二个样式表:"tool_updated.xsl"。这是用来显示最新的XML数据的XSL文件。这个样式表不会产生一个可编辑HTML表格,但是会产生一个静态的HTML表格:
<?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> |
The ASP File
ASP文件
The HTML form in the "tool.xsl" file above has an action attribute with a value of "edittool.asp".
上面"tool.xsl" 文件中,HTML的构成具有"edittool.asp"值的行为属性。
The "edittool.asp" page contains two functions: The loadFile() function loads and transforms the XML file for display and the updateFile() function applies the changes to the XML file:
"edittool.asp"页包含了2个函数:loadFile()函数是用来加载和转换XML文件的,目的在于将其显示出来;updateFile()函数的作用是把所有的这些改变应用于XML文件。
<% 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 ASP, you can study our ASP tutorial.
提示: 如果你还不会写ASP文件的话请学习我们的ASP 教程.
Note: We are doing the transformation and applying the changes to the XML file on the server. This is a cross-browser solution. The client will only get HTML back from the server - which will work in any browser.
注意: 我们可以进行转换,并把这些改变应用到在服务器上的XML文件。这将是一个交互式浏览器的解决方案。客户端只要从浏览器获取HTML值就可以了,而且所有的浏览器都具备这样的功能。