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

4
  • Your question is unclear. Could you elaborate and maybe give an example? Commented Sep 1, 2010 at 15:58
  • 2
    I don't understand what you mean. Commented Sep 1, 2010 at 15:59
  • what language you're using behind the html? or you jsut want to do it in js? where is located the xml? is it an external file? or you want to generate xml content?and the most important question.. where's waldo? Commented Sep 1, 2010 at 16:02
  • Good question (+1). See my answer for a solution that is completely in the spirit of XSLT. Commented Sep 1, 2010 at 17:02

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.