XSLT - Editar XML
Los datos almacenados en archivos XML pueden editarse mediante un navegador de Internet.
Abrir, editar y guardar XML
Ahora le mostraremos cómo abrir, editar y guardar archivos XML almacenados en el servidor.
Usaremos XSL para convertir el documento XML en un formulario HTML. Los valores de los elementos XML se escribirán en los campos de entrada HTML del formulario HTML. Este formulario HTML es editable. Una vez editado, los datos se envían de vuelta al servidor y el archivo XML se actualiza (esta parte la realiza ASP).
Archivos XML y archivos XSL
Primero, vea el documento XML que se utilizará ("tool.xml"):
<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>
。
A continuación, vea la siguiente hoja de estilo ("tool.xsl"):
<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>
。
El archivo XSL anterior recorre en bucle los elementos del archivo XML y crea un campo de entrada para cada elemento XML "field". El valor del atributo "id" del elemento XML "field" se añade a los atributos "id" y "name" de cada campo de entrada HTML. El valor de cada elemento XML "value" se añade al atributo "value" de cada campo de entrada HTML. El resultado es un formulario HTML editable que contiene los valores del archivo XML.
Luego, tenemos una segunda hoja de estilo: "tool_updated.xsl". Este archivo XSL se utiliza para mostrar los datos XML actualizados. Esta hoja de estilo no genera un formulario HTML editable, sino una tabla HTML estática:
<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>
。
Archivos ASP
En el archivo "tool.xsl" anterior, el valor del atributo action del formulario HTML es "edittool.asp".
La página "edittool.asp" contiene dos funciones: la función loadFile() carga y transforma el archivo XML, y la función updateFile() actualiza el archivo 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
%>
Consejo:Si no sabe cómo escribir ASP, consulte nuestroTutorial de ASP。
Nota:Estamos convirtiendo y actualizando el archivo XML ubicado en el servidor. Esta es una solución multiplataforma. El cliente solo recibe el HTML devuelto por el servidor, y el HTML puede ejecutarse en cualquier navegador.
Otras extensiones