选择显示字体大小

用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>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中的值相匹配。

 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons