# 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 文件](/try/xml/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 文件](/try/xml/tool.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 文件](/try/xml/tool_updated.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 教程](/asp/asp-tutorial.html)。
**注意:**我們正在轉換并更新位于服務器上的 XML 文件。這是一個跨平臺的解決方案。客戶端僅能獲得從服務器返回的 HTML - 而 HTML 可運行于任何瀏覽器。
- XML 基礎
- XML 簡介
- XML 用途
- XML 樹結構
- XML 語法規則
- XML 元素
- XML 屬性
- XML 驗證
- 查看 XML 文件
- 使用 CSS 顯示 XML
- 使用 XSLT 顯示 XML
- XML Javascript
- XMLHttpRequest 對象
- XML Parser
- XML DOM
- XML to HTML
- XML 應用程序
- XML 進階
- XML 命名空間
- XML CDATA
- XML 編碼
- 服務器上的 XML
- XML DOM 高級
- XML 注意事項
- XML 相關技術
- 現實生活中的 XML
- XML 編輯器
- XML - E4X
- DTD 教程
- DTD 簡介
- DTD - XML 構建模塊
- DTD - 元素
- DTD - 屬性
- XML 元素 vs. 屬性
- DTD - 實體
- DTD 驗證
- DTD - 來自網絡的實例
- XML DOM
- XML DOM 簡介
- XML DOM 節點
- XML DOM 節點樹
- XML DOM 解析器
- XML DOM 加載函數
- XML DOM - 屬性和方法
- XML DOM - 訪問節點
- XML DOM 節點信息
- XML DOM 節點列表
- XML DOM 遍歷節點樹
- XML DOM 瀏覽器差異
- XML DOM - 導航節點
- XML DOM 獲取節點值
- XML DOM 改變節點值
- XML DOM 刪除節點
- XML DOM 替換節點
- XML DOM 創建節點
- XML DOM 添加節點
- XML DOM 克隆節點
- The XMLHttpRequest 對象
- XML DOM 節點類型
- XML DOM - Node 對象
- XML DOM - NodeList 對象
- XML DOM - NamedNodeMap 對象
- XML DOM - Document 對象
- XML DOM - DocumentImplementation 對象
- XML DOM - DocumentType 對象
- XML DOM - ProcessingInstruction 對象
- XML DOM - Element 對象
- XML DOM - Attr 對象
- XML DOM - Text 對象
- XML DOM - CDATASection 對象
- XML DOM - Comment 對象
- XMLHttpRequest 對象
- XML DOM Parse Error 對象
- XML DOM 解析器錯誤
- XSLT 教程
- XSL 語言
- XSLT 簡介
- XSLT 瀏覽器
- XSLT - 轉換
- XSLT <xsl:template> 元素
- XSLT <xsl:value-of> 元素
- XSLT <xsl:for-each> 元素
- XSLT <xsl:sort> 元素
- XSLT <xsl:if> 元素
- XSLT <xsl:choose> 元素
- XSLT <xsl:apply-templates> 元素
- XSLT - 在客戶端
- XSLT - 在服務器端
- XSLT - 編輯 XML
- XML 編輯器
- XSLT 元素參考手冊
- XSLT 函數
- XPath 教程
- XPath 簡介
- XPath 節點
- XPath 語法
- XPath 軸(Axes)
- XPath 運算符
- XPath Examples
- XPath、XQuery 以及 XSLT 函數函數參考手冊
- 函數參考手冊
- XQuery 教程
- XQuery 簡介
- XQuery 實例
- XQuery FLWOR 表達式
- XQuery FLWOR + HTML
- XQuery 術語
- XQuery 語法
- XQuery 添加元素 和屬性
- XQuery 選擇 和 過濾
- XQuery 函數
- XQuery 參考手冊
- XLink 和 XPointer 教程
- XLink 和 XPointer 簡介
- XLink 和 XPointer 語法
- XLink 實例
- XPointer 實例
- XLink 參考手冊
- XML Schema 教程
- XML Schema 簡介
- 為什么使用 XML Schemas?
- XSD 如何使用?
- XSD - <schema> 元素
- XSD 簡易元素
- XSD 屬性
- XSD 限定 / Facets
- XSD 復合元素
- XSD 空元素
- XSD 僅含元素
- XSD 僅含文本
- XSD 混合內容
- XSD 指示器
- XSD <any> 元素
- XSD <anyAttribute> 元素
- XSD 元素替換(Element Substitution)
- XSD 實例
- XSD 字符串 數據類型
- XSD 日期和時間數據類型
- XSD 數值數據類型
- XSD 雜項 數據類型
- XML 編輯器
- XML Schema 參考手冊
- XSD 元素
- XSD 限定/Facets
- SOAP 教程
- SOAP 簡介
- SOAP 語法
- SOAP Envelope 元素
- SOAP Header 元素
- SOAP Body 元素
- SOAP Fault 元素
- SOAP HTTP 協議
- SOAP 實例
- WSDL 教程
- WSDL 簡介
- WSDL 文檔
- WSDL 端口
- WSDL 綁定
- WSDL UDDI
- RSS 教程
- RSS 簡介
- RSS 歷史
- RSS 語法
- RSS <channel> 元素
- RSS <item> 元素
- RSS 發布您的 Feed
- RSS 閱讀器
- RSS 參考手冊
- RDF 教程
- RDF 簡介
- RDF 規則
- RDF 實例
- RDF 主要 元素
- RDF 容器 Elements
- RDF 集合
- RDF Schema (RDFS)
- RDF 都柏林核心元數據倡議
- OWL 簡介
- RDF 參考手冊
- XSL-FO 教程
- XSL-FO 簡介
- XSL-FO 文檔
- XSL-FO 區域
- XSL-FO 輸出
- XSL-FO 流
- XSL-FO 頁面
- XSL-FO 塊
- XSL-FO 列表
- XSL-FO 表格
- XSL-FO 與 XSLT
- XSL-FO 軟件
- XSL-FO 參考手冊
- SVG 教程
- SVG 簡介
- SVG 實例
- SVG 在 HTML 頁面
- SVG <rect>
- SVG <circle>
- SVG <ellipse>
- SVG <line>
- SVG <polygon>
- SVG <polyline>
- SVG <path>
- SVG <text>
- SVG Stroke 屬性
- SVG 濾鏡
- SVG 模糊效果
- SVG 陰影
- SVG 漸變 - 線性
- SVG 漸變- 放射性
- SVG 參考手冊
- 免責聲明