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

Mar 28, 2017 pm 05:05 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Use PHP and XML to process and display geolocation and map data Use PHP and XML to process and display geolocation and map data Aug 01, 2023 am 08:37 AM

Processing and displaying geolocation and map data using PHP and XML Overview: Processing and displaying geolocation and map data is a common requirement when developing web applications. PHP is a popular server-side programming language that can interact with data in XML format. This article explains how to use PHP and XML to process and display geolocation and map data, and provides some sample code. 1. Preparation: Before starting, you need to ensure that PHP and related extensions, such as Simple, are installed on the server

See all articles