Search code examples
htmlxmlformsxslt

How do I produce a HTML form using XSLT?


I have a form to fill out:

<form action="welcome.jsp"  method="post">
 <table>
  <tr><td>Email:</td><td><input type="text" name="email"></td></tr>
  <tr><td>Name:</td><td><input type="text" name="name"></td></tr>
  <tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr>
  <tr><td></td><td><input type="submit" value="Submit"></td></tr>
 </table>
</form>

However, how do I produce the same form using XSLT? This form resides in a index.jsp file and I have the xml in this file any mockup xml for now can be used, I am mostly confused in

<input ... > 

part.

thanks


Solution

  • This XML input file:

    <r>
      <email>[email protected]</email>
      <name>Bob</name>
      <mobile>123-456-7890</mobile>
    </r>
    

    Fed to this XSLT transformation:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" 
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" indent="yes" />
      <xsl:template match="/r">
        <xsl:variable name="email" select="email"/>
        <xsl:variable name="name" select="name"/>
        <xsl:variable name="mobile" select="mobile"/>
        <form action="welcome.jsp"  method="post">
          <table>
            <tr><td>Email:</td><td><input type="text" name="email" value="{$email}"></input></td></tr>
            <tr><td>Name:</td><td><input type="text" name="name" value="{$name}"/></td></tr>
            <tr><td>Mobile:</td><td><input type="text" name="mobile" value="{$mobile}"/></td></tr>
            <tr><td></td><td><input type="submit" value="Submit"/></td></tr>
          </table>
        </form>
      </xsl:template>
    </xsl:stylesheet>
    

    Yields this HTML of the completed form:

    <form action="welcome.jsp" method="post">
       <table>
          <tr>
             <td>Email:</td>
             <td><input type="text" name="email" value="[email protected]"></td>
          </tr>
          <tr>
             <td>Name:</td>
             <td><input type="text" name="name" value="Bob"></td>
          </tr>
          <tr>
             <td>Mobile:</td>
             <td><input type="text" name="mobile" value="123-456-7890"></td>
          </tr>
          <tr>
             <td></td>
             <td><input type="submit" value="Submit"></td>
          </tr>
       </table>
    </form>
    

    Which looks like this:

    enter image description here