非常教程

XSLFO 教程教程

XSLT – 编辑 XML

XSLT – 编辑 XML

XSLT - 编辑 XML


存储在 XML 文件中的数据可通过因特网浏览器进行编辑。


打开、编辑并保存 XML

现在,我们会为您展示如何打开、编辑及保存存储于服务器上的 XML 文件。

我们将使用 XSL 把 XML 文档转换到一个 HTML 表单中。XML 元素的值会被写到 HTML 表单中的 HTML 输入域。这个 HTML 表单是可编辑的。在被编辑完成后,数据会被提交回服务器,XML 文件会得到更新(这部分由 ASP 完成)。


XML 文件和 XSL 文件

首先,请看将被使用的 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>

查看 XML 文件。

接着,请看下面的样式表("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>

查看 XSL 文件。

上面这个 XSL 文件会循环遍历 XML 文件中的元素,并为每个 XML "field" 元素创建一个输入域。XML "field" 元素的 "id" 属性的值被添加到每个 HTML 输入域的 "id" 和 "name" 属性。每个 XML "value" 元素的值被添加到每个 HTML 输入域的 "value" 属性。结果是,可以得到一个包含 XML 文件中值的可编辑的 HTML 表单。

然后,我们还有第二个样式表:"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>

查看 XSL 文件。


ASP 文件

在上面 "tool.xsl" 文件中,HTML 表单的 action 属性的值是 "edittool.asp" 。

"edittool.asp" 页面包含两个函数: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
%>

提示:假如您不了解如何编写 ASP,请学习我们的 ASP 教程。

注意:我们正在转换并更新位于服务器上的 XML 文件。这是一个跨平台的解决方案。客户端仅能获得从服务器返回的 HTML - 而 HTML 可运行于任何浏览器。


XSLT – 编辑 XML
XSLFO 教程

XSL-FO,即可扩展样式表语言格式化对象(Extensible Stylesheet Language Formatting Objects),是用于格式化 XML 数据的语言。XSL-FO 是一个 W3C 推荐标准。

XSLFO 教程目录

1.XSLT 教程
2.XSL-FO 教程
3.XSLT <xsl:value-of> 元素
4.XSLT <xsl:template> 元素
5.XSLT 转换
6.XSLT 浏览器
7.XSLT 简介
8.XSL 语言
9.XSLT 在服务器端
10.XSLT 在客户端
11.XSLT <xsl:apply-templates> 元素
12.XSLT <xsl:choose> 元素
13.XSLT <xsl:if> 元素
14.XSLT <xsl:sort> 元素
15.XSLT <xsl:for-each> 元素
16.XSLT 总结
17.XML 编辑器
18.XSLT – 编辑 XML
19.XSLT <xsl:comment> 元素
20.XSLT <xsl:choose> 元素
21.XSLT <xsl:call-template> 元素
22.XSLT <xsl:attribute-set> 元素
23.XSLT <xsl:attribute> 元素
24.XSLT <xsl:apply-templates> 元素
25.XSLT <xsl:apply-imports> 元素
26.XSLT 实例
27.XSLT <xsl:import> 元素
28.XSLT <xsl:if> 元素
29.XSLT <xsl:for-each> 元素
30.XSLT <xsl:fallback> 元素
31.XSLT <xsl:element> 元素
32.XSLT <xsl:decimal-format> 元素
33.XSLT <xsl:copy-of> 元素
34.XSLT <xsl:copy> 元素
35.XSLT <xsl:otherwise> 元素
36.XSLT <xsl:number> 元素
37.XSLT <xsl:namespace-alias> 元素
38.XSLT <xsl:message> 元素
39.XSLT <xsl:key> 元素
40.XSLT <xsl:include> 元素
41.XSLT <xsl:variable> 元素
42.XSLT <xsl:value-of> 元素
43.XSLT <xsl:text> 元素
44.XSLT <xsl:template> 元素
45.XSLT <xsl:stylesheet> 和 <xsl:transform> 元素
46.XSLT <xsl:sort> 元素
47.XSLT <xsl:processing-instruction> 元素
48.XSLT <xsl:preserve-space> 和 <xsl:strip-space> 元素
49.XSLT <xsl:param> 元素
50.XSLT <xsl:output> 元素
51.XSLT key() 函数
52.XSLT generate-id() 函数
53.XSLT function-available() 函数
54.XSLT format-number() 函数
55.XSLT element-available() 函数
56.XSLT document() 函数
57.XSLT current() 函数
58.XSLT 元素参考手册
59.XSLT <xsl:with-param> 元素
60.XSLT <xsl:when> 元素
61.XSL-FO 流
62.XSL-FO 输出
63.XSL-FO 区域属性
64.XSL-FO 文档
65.XSL-FO 简介
66.XSLT 函数
67.XSLT unparsed-entity-uri() 函数
68.XSLT system-property() 函数
69.XSL-FO 软件
70.XSL-FO 与 XSLT
71.XSL-FO 表格
72.XSL-FO 列表
73.XSL-FO block 对象
74.XSL-FO bidi-override 对象
75.XSL-FO basic-link 对象
76.XSL-FO flow 对象
77.XSL-FO float 对象
78.XSL-FO external-graphic 对象
79.XSL-FO declarations 对象
80.XSL-FO conditional-page-master-reference 对象
81.XSL-FO color-profile 对象
82.XSL-FO character 对象
83.XSL-FO block-container 对象
84.XSL-FO inline-container 对象
85.XSL-FO inline 对象
86.XSL-FO initial-property-set 对象
87.XSL-FO footnote-body 对象
88.XSL-FO footnote 对象
89.XSL-FO list-item-label 对象
90.XSL-FO list-item-body 对象
91.XSL-FO list-item 对象
92.XSL-FO list-block 对象
93.XSL-FO leader 对象
94.XSL-FO layout-master-set 对象
95.XSL-FO instream-foreign-object 对象
96.XSL-FO multi-case 对象
97.XSL-FO marker 对象
98.XSL-FO region-before 对象
99.XSL-FO region-after 对象
100.XSL-FO page-sequence-master 对象