代码之家  ›  专栏  ›  技术社区  ›  Praneel PIDIKITI

使用简单的HTML表单编写XML

  •  1
  • Praneel PIDIKITI  · 技术社区  · 16 年前

    是否有方法将表单中的值使用到XML节点中?提交此表单后,我希望表单字段中的值覆盖现有节点。

    <html>
     <head></head>
     <body>
     <form id="myForm" method="POST">
     <input type="text" value="new XML node value here" />
     <input type="submit" onClick="function();"/>
     </form>
     </body>
    

    提前谢谢

    1 回复  |  直到 16 年前
        1
  •  2
  •   Dimitre Novatchev    16 年前

    这种转变 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:ext="http://exslt.org/common">
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
      <xsl:param name="pqueryString">
       <s name="field1">Hello world!</s>
      </xsl:param>
    
      <xsl:variable name="vqs"
       select="msxsl:node-set($pqueryString)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="input[@type='text']/@value">
       <xsl:attribute name="value">
         <xsl:value-of select=
          "../@value[not($vqs/s[@name = current()/../@name])]
          |
           $vqs/s[@name = current()/../@name]
          "/>
       </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于此XML文档时 (更正了提供的格式):

    <html>
        <head></head>
        <body>
            <form id="myForm" method="POST">
                <input name="field1" type="text" value="default" />
                <input type="submit" onClick="function();"/>
            </form>
        </body>
    </html>
    

    产生想要的、正确的结果 :

    <html>
        <head></head>
        <body>
            <form id="myForm" method="POST">
                <input name="field1" type="text" value="Hello world!"></input>
                <input type="submit" onClick="function();"></input>
            </form>
        </body>
    </html>
    

    做笔记 :

    1. 使用标识规则 按原样复制文件。

    2. HTTP请求的查询字符串作为外部参数传递 命名 pqueryString .

    3. 这个 ext:node-set() 实践中不需要这里使用的扩展 ,因为参数将从外部传递。

    4. 对标识规则的唯一重写是对名为 value .

    5. 模板匹配 @value 创建具有相同名称的属性,其值为 用户指定的值(包含在查询字符串参数中),或者如果用户没有为此属性指定值,则为其当前值。