当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

用XSL.ASP编辑XML文档

  简介

  本文是"保存至HTML 表格数据至XML"的姐妹篇。如果你没读过上文,我建议您最好先浏览一下。本文是建立在上文基础之上的。关于上文的举例,读者不断给予了肯定的回应,同样的,很多人都想知道如何编辑XML数据。因此,我写下了此文。

  使用XSL状态下:打开一个XML文件,确定将对它进行编辑、传送至HTML表单,并最终将传送到浏览器。 此XML元素的值将会被设置成HTML输入域的值。在这些必要的编辑后,则可将这些经处理的信息提交至服务器,XML文件同时也被更新。

  第一步LOAD你将会编辑并在浏览器以HTML表格形式出现的文件。在以下的举例中,XML在服务器上的变化被我跳过了,在使用微软的XMLDOM 目标下,XML文件是能被XSL文件转化的。我们在此同样可以用到这个技巧来转化XML文件。

  XML File: contact.xml:
  <?xml version="1.0" ?>
  <contact>
   <field id="firstName" taborder="1">
    <field_value>Michael</field_value>
   </field>
   <field id="lastName" taborder="2">
    <field_value>Qualls</field_value>
   </field>
   <field id="address1" taborder="3">
    <field_value>202 East Haverbrook</field_value>
   </field>
   <field id="address2" taborder="4">
    <field_value>Oklahoma City, OK 73114</field_value>
   </field>
   <field id="phone" taborder="5">
    <field_value>4055551234</field_value>
   </field>
   <field id="email" taborder="6">
    <field_value>[email protected]</field_value>
   </field>
  </contact>
  本文举例用到的XML文件与 "保存HTML表格至XML"一文中的举例一样。因此你能够更直观的观察到其中的关联之处。

  XSL File: contact.xsl:

  <?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
  <html>
  <body>
   <form method="post" action="EditContact.asp">
   <h1>Edit Contact:</h1>
   <table border="1" cellpadding="2">
   <xsl:for-each select="contact/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>
   <input type="submit" id="btnSubmit" name="btnSubmit" value="Submit" />
   </form>
   </body>
   </html>
   </xsl:template>
   </xsl:stylesheet>
  这个XSL文件使用了for-each XSL元素,使之在XSL文件的元素中反复。

  由此根元素开始,每个XML"域"元素的"ID"被写成了HTML文本域的"ID"和"NAME"。

  同样,XML文件中"域值/FIELD_VALUE"元素的值也被写成为每个HTML文本域中的"值/value"。最后的结果自然是HTML格式包含了来自XML文件中将会被编辑的值。

  我之所以把"ID"从XML文件中的"域"元素里提出来,并把它置于XSL文件中的HTML文本域中,是为了不至于混淆并且可以促进命名的连贯性。这样的话,不太熟悉编码知识的朋友也能分辨出哪个XML域配哪 个HTML域。

  通过使用上述两个文件,我们已为开始编辑XML文件做好了充分准备。XSL文件将会传输XML文件以便能够在浏览器上显示。我们可以在终端机上做这个传输工作,但不是最好的解决方案。用ASP的话,我们可以在服务器上做这个传输工作。同样的,我们可以在服务器上做XML文件的编辑工作。
例子:通过使用XSL,ASP来编辑XML

  编辑 Contact.asp 是一个比较普遍的现象。这儿有两个功能在编辑ASP页面中起了主要作用。第一个是loadXMLFile功能,它LOAD并传输XML文件使之显示出来;第二个是 updateXML 功能,它适用于编辑 XML文件国。

  ASP File: EditContact.asp:

   <%
    '-----------------------------------------------------------
    '"loadXMLFile" 函数接受两个参数.
    'strXMLFile - XML文件的路径名和文件名.
    'strXSLFilee - XSL文件的路径名和文件名.
    '-----------------------------------------------------------
    Function loadXMLFile(strXMLFile, strXSLFile)
     '本地变量

     Dim objXML Dim objXSL
     '初始化XMLDOM对象.

     set objXML = Server.CreateObject("Microsoft.XMLDOM")
     '关闭同步加载的文件.
     objXML.async = false

     '加载XML文件.

     objXML.load(strXMLFile)
     '初始化用于加载XSL文件的XMLDOM对象.
     set objXSL = Server.CreateObject("Microsoft.XMLDOM")
     'Turn off asyncronous file loading.
     objXSL.async = false 'Load the XSL file.
     objXSL.load(strXSLFile)
     'Use the "transformNode" method of the XMLDOM to apply the
      'XSL stylesheet to the XML document. Then the output is
     'written to the client.
     Response.Write(objXML.transformNode(objXSL))
    End Function
    '-----------------------------------------------------------
    'The "updateXML" Function accepts one parameter.
    'strXMLFile - The path and file name of the XML file.
    '-----------------------------------------------------------

   Function updateXML(strXMLFile)
    'Declare local variables.
    Dim objDom
    Dim objRoot
    Dim objField
    Dim x
    'Instantiate the XMLDOM Object.
    set objDOM = Server.CreateObject("Microsoft.XMLDOM")
    'Turn off asyncronous file loading.
    objDOM.async = false
    'Load the XML file.
    objDOM.load strXMLFile
    'Set the objRoot variable equal to the root element of the
    'XML file by calling the documentElement method of the
    'objDOM (XMLDOM) object.
    Set objRoot = objDom.documentElement
    'Iterate through the Form Collection and write the
    'submitted values to the XML file.
    For x = 1 to Request.Form.Count
    'Check see if "btn" is in the submitted value, if so,
    'it is a button and should be ignored.
    If instr(1,Request.Form.Key(x),"btn") = 0 Then
    'Set objField variable equal to a field_value element by
    'calling the selectSingleNode method of the objRoot
    '(documentElement) object. The SelectSingleNode method
    'accepts a string parameter for querying the XML document.
    'In this case, the current value of the key property of
    'the Form Collection is used to find the appropriate
    'field_value element (more on this later).
    
    Set objField = objRoot.selectSingleNode("field[@id='" & _ Request.Form.Key(x) & "']/field_value")
    'Set the text property of the objField (field_value)
    'element equal to the value of the current form field.
    objField.Text = Request.Form(x)
   End If
   Next
   'After the XML file has been edited, is must be saved.
   objDom.save strXMLFile
   'Release all of your object references.
   Set objDom = Nothing
   Set objRoot = Nothing
   Set objField = Nothing
   'Call the loadXMLFile method, passing in the newly edited
   'XML file and the updatedcontact.xsl style sheet. This will
   'allow the client to see the edited information. More on the
   'updatedcontact.xsl file later.
   loadXMLFile strXMLFile,
   server.MapPath("updatedcontact.xsl")
  End Function
   'Test to see if the form has been submitted. If it has,
   'update the XML file. If not, transform the XML file for
   'editing.
  If Request.Form("btnSubmit") = "" Then
   loadXMLFile server.MapPath("Contact.xml"), _ server.MapPath("contact.xsl")
  Else
   updateXML server.MapPath("Contact.xml")
  End If
  %>

  正如你所看到的一样,ASP文件处理了整个XML文件更新的过程。如果表单已被提交,那么XML文件则会被打开并更新。如果表单没有被提交,那么XML文件会由contact.xsl传送至HTML格式,以便用户自行编辑。详见以下举例:

  For x = 1 to Request.Form.Count
   If instr(1,Request.Form.Key(x),"btn") = 0 Then
    Set objField = objRoot.selectSingleNode("field[@id='" & _ Request.Form.Key(x) & "']/field_value")
    objField.Text = Request.Form(x)
   End If
  Next
 
  上述代码是更新XML文件的代码。SelectSingleNode 方法是关键。


  在上述举例中,问句是"field[@id='"& request.form.key(x) & "']/field_value"。所询问的是:要求做为 子域元素 的field_value element 包含一个"ID",此ID而且是与现有的Form Collection中的关键值相匹配。一旦获得适当的节点,则可以更新文本属性以便与Form Collection中的值相匹配。

 
相关内容
赞助商链接