Home > Backend Development > XML/RSS Tutorial > Detailed code explanation for online editing of XML documents using XSL and ASP

Detailed code explanation for online editing of XML documents using XSL and ASP

黄舟
Release: 2017-03-28 17:05:59
Original
1474 people have browsed it

This article explains the method of online editing of XML document data through a detailed example. Since Netscape's support for XML is relatively weak, to achieve cross-platform data exchange, data processing must be performed on the server side. To edit an XML document, the first thing to do is how to extract and display the data to visitors. XSL provides a good solution for us to display XML files. The following example uses an XSL style sheet to display an XML document for users to edit, and then submits the edited data to the server, where the data is processed on the server sideUpdate. Here we use ASP (Active Server Pages) to complete our tasks.

First of all , load the XML document we want to edit, and using Microsoft DocumentObjectModel (Microsoft XMLDOM Object) and XSL, the XML document can be processed on the server side Convert it into HTML file content that can be displayed on the client. Let's take a look at what the XML and XSL files we use look like.

XML file: userdata.xml

<?xml version="1.0" encoding="gb2312"?>
<用户资料>
<field id="姓名" taborder="1">
<field_value>孟子</field_value>
</field>
<field id="性别" taborder="2">
<field_value>男</field_value>
</field>
<field id="单位名称" taborder="3">
<field_value>中国网络技术发展公司北京分公司</field_value>
</field>
<field id="详细地址" taborder="4">
<field_value>北京市嘉里中心102层</field_value>
</field>
<field id="电话" taborder="5">
<field_value>1391139136*</field_value>
</field>
<field id="电子邮件" taborder="6">
<field_value>amxh@21cn.com</field_value>
</field>
</用户资料>
XSL文件:userdata.xsl
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body>
<form method="post" action="Edituserdata.asp">
<h1>用户资料编辑:</h1>
<table border="1" cellpadding="2">
<xsl:for-each select="用户资料/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="field_value" /> 
</xsl:attribute></input>
</td>
</tr>
</xsl:for-each>
</table>
<br />
<input type="submit" id="btnSubmit" name="btnSubmit" value="完成编辑" />
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Copy after login

XSL file usage. The XSL: for-each element is used to traverse the entire XML file. The "id" attribute of each "field" element in the XML file and the "id" of the text input box of the HTML form " and "name" want to correspond. In this way, the text input box of the HTML form displays the element value of the XML file. This file is responsible for converting the XML document on the server side so that it can be displayed on various browsers.

The following is the key program, which implements the function of opening and updating XML documents, and determines whether to update based on whether the form is submitted. It contains two functions, loadXMLFile, which is responsible for loading and converting the XML file to be displayed. ;The updateXML function is responsible for updating the XML file.

The Edituserdata.asp program is as follows:

<%
&#39;-----------------------------------------------------------
&#39;定义函数 loadXMLFile(),接收二个参数:
&#39;strXMLFile - XML 文件的路径和文件名字
&#39;strXSLFilee - XSL 文件的路径和文件名字
&#39;-----------------------------------------------------------
Function loadXMLFile(strXMLFile, strXSLFile)
&#39;Declare local variables
Dim objXML
Dim objXSL
&#39;实例化 XMLDOM 对象,以便载入 XML 文件。
set objXML = Server.CreateObject("Microsoft.XMLDOM")
&#39;关掉文件异步载入模式。
objXML.async = false
&#39;载入 XML 文件!
objXML.load(strXMLFile)
&#39;实例化 XMLDOM 对象,以便载入 XSL 文件。
set objXSL = Server.CreateObject("Microsoft.XMLDOM")
&#39;关掉文件异步载入模式。
objXSL.async = false
&#39;载入 XSL 文件!
objXSL.load(strXSLFile)
&#39;利用 XMLDOM 的 transformNode 方法,把 XSL 样式表应用到 XML 文档,然后输出到客户端。
Response.Write(objXML.transformNode(objXSL))
End Function
&#39;------------------------------------------------------------------
&#39;函数 updateXML() 接收一个参数:strXMLFile - XML 文件的路径和文件名。
&#39;------------------------------------------------------------------
Function updateXML(strXMLFile)
&#39;声明局部变量。
Dim objDom
Dim objRoot
Dim objField
Dim x
&#39;实例化 XMLDOM 对象。
set objDOM = Server.CreateObject("Microsoft.XMLDOM")
&#39;关掉文件异步载入模式。
objDOM.async = false
&#39;载入 XML 文件。
objDOM.load strXMLFile
&#39;设定根元素。
Set objRoot = objDom.documentElement
&#39;遍历 FORM 集合,并把提交的数据写入 XML 文件。
For x = 1 to Request.Form.Count
&#39;检查提交的数据是否包含按钮。如果是,忽略此数据。
If instr(1,Request.Form.Key(x),"btn") = 0 Then
&#39;按照 XSL 查询模式,建立 objField 变量,把表单的元素对应到 XML 文档里的相应元素[field_value]。
Set objField = objRoot.selectSingleNode("field[@id=&#39;" & Request.Form.Key(x) & "&#39;]/field_value")
&#39;把表单提交的数据和 XML 文档里的节点值对应起来。
objField.Text = Request.Form(x)
End If
Next
&#39;保存编辑过的 XML 文件。
objDom.save strXMLFile
&#39;释放所有对对象的引用。
Set objDom = Nothing
Set objRoot = Nothing
Set objField = Nothing
&#39;调用 loadXMLFile 函数,把新编辑后的 XML 文件用 updateduserdata.xsl 样式单显示到客户端。
loadXMLFile strXMLFile,server.MapPath("updateduserdata.xsl")
End Function
&#39;检查表单是否成功提交,如提交,更新 XML 文件;否则,转到编辑状态。
If Request.Form("btnSubmit") = "" Then
loadXMLFile server.MapPath("userdata.xml"),server.MapPath("userdata.xsl")
Else
updateXML server.MapPath("userdata.xml")
End If
%>
Copy after login

When the form is submitted successfully, we use updateduserdata.xsl to display the data we just edited. ##updateduserdata.xsl is as follows:

<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body>
<h1>更新后的用户资料如下:</h1>
<table border="1" cellpadding="2">
<xsl:for-each select="用户资料/field">
<tr>
<td>
<xsl:value-of select="@id" />
</td>
<td>
<xsl:value-of select="field_value" />
</td>
</tr>
</xsl:for-each>
</table>
<form>
<input type="button" value="返回" onclick="history.go(-1)" />
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Copy after login

The above is the detailed content of Detailed code explanation for online editing of XML documents using XSL and ASP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template