用XSL.ASP編輯XML文檔

來源:互聯網
上載者:User
簡介

  本文是"儲存至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>mqualls@vertiscope.com</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中的值相匹配。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.