当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT - 编辑XML

XSL/XSLT
XSL 语言
XSLT 介绍
XSLT 浏览器
XSLT 转换
<xsl:template>
<xsl:value-of>
<xsl:for-each>
<xsl:sort>
<xsl:if>
<xsl:choose>
<xsl:apply-templates>
XSLT - 客户端
XSLT - 服务器端
XSLT - 编辑XML
XSLT 摘要
XSLT 元素参考
XSLT 函数
XSL 编辑器

XSL/XSLT 中的 XSLT - 编辑XML


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 416 ::
收藏到网摘: n/a

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输入域。HTML的格式是可以被编辑的。数据被编辑之后将被重新传回服务器,XML也将被升级(这部分工作由ASP完成)。


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>

View the XML file.
XML文件.

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>

View the XSL file.
XSL文件.

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: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>

View the XSL file.
XSL文件.


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"值的行为(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文件中。

<%
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值就可以了,而且所有的浏览器都具备这样的功能。

评论 (0) All

登陆 | 还没注册?