DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 用XSL.ASP編輯XML文檔
用XSL.ASP編輯XML文檔
編輯: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中的值相匹配。

 

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved