当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT - 编辑XML
Data stored in XML files can be edited from an Internet browser.
储存在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输入域。HTML的格式是可以被编辑的。数据被编辑之后将被重新传回服务器,XML也将被升级(这部分工作由ASP完成)。
First, look at the XML document that will be used ("tool.xml"):
首先,看一下需要用到的 XML文件("tool.xml"):
<?xml version="1.0" encoding="ISO-8859-1"?> |
Then, take a look at the following style sheet ("tool.xsl"):
让我们看一下下面这张样式表文件("tool.xsl"):
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> |
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”和“名称(name)”属性中。每个XML“值(value)”元素的值要被添加到每个HTML输入域的“值(value)”属性里。结果则是一个包含XML文件值的一张可编辑的HTML表单。
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"。这就是XSL文件,它用于显示最新的XML数据。这个样式表不会产生一个可编辑HTML表格,但是会输出一个静态的HTML表格:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> </xsl:stylesheet> |
The HTML form in the "tool.xsl" file above has an action attribute with a value of "edittool.asp".
上述"tool.xsl" 文件中的HTML表单里含有一个附带"edittool.asp"值的行为(action)属性。
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文件中。
<% 'Load XML file 'Load XSL file 'Transform file function updateFile(xmlfile) 'Load XML file 'Set the rootEl variable equal to the root element 'Loop through the form collection 'Save the modified XML file 'Release all object references 'Load the modified XML file with a style sheet that 'If the form has been submitted update the |
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值就可以了,而且所有的浏览器都具备这样的功能。