tags:

views:

35

answers:

1

Is there a way to use the value from the form into XML nodes? Once they submit this form I want the values from the form fields to overwrite the existing nodes.

<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>

Thanks in advance

+3  A: 

This transformation:

<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"&gt;
 <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>

when applied on this XML document (corrected the provided to be well-formed):

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

produces the wanted, correct result:

<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>

Do note:

  1. The Identity rule is used to copy the document as is.

  2. The query string of the HTTP request is passed as an external parameter named pqueryString.

  3. The ext:node-set() extension used here will not be needed in practice, because the parameter will be passed externally.

  4. The only override of the identity rule is for attributes named value.

  5. The template matching @value creates an attribute with the same name and its value is either the one specified by the user (contained in the query string param) or if the user didn't specify a value for this attribute, then its current value.

Dimitre Novatchev
@Dimitre: +1 Good answer and good looking at!
Alejandro
@Dimitre Novatchev : Thanx a ton .. its perfect :)
Praneel