XML technologies / XSLT / XSLT Kochbuch / Code generation / Generating a web client for entering test data

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You want to test a process in isolation by entering information in a form, converting it into a message and sending it to the process.

Solution

This example generates the client side of a test-data entry tool: an HTML form for entering message fields. Its data is processed by a CGI program generated in the recipe Generating web CGI for test input:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:param name="message"/>
  <!-- Ein Schlüssel, um Datentypen über den Namen zu finden. -->
  <xsl:key name="dataTypes" match="Structure" use="Name" />
  <xsl:key name="dataTypes" match="Primitive" use="Name" />
  <xsl:key name="dataTypes" match="Array" use="Name" />
  <xsl:key name="dataTypes" match="Enumeration" use="Name" />
  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="$message"/> Entry</title>
      </head>
      <body bgcolor="#FFFFFF" text="#000000">
        <h1><xsl:value-of select="$message"/> Entry</h1>
        <form name="{concat($message,'Form')}" method="post" action="{concat('/cgi-bin/',$message,'Process.pl')}">
          <xsl:apply-templates select="*/Messages/Message[Name=$message]"/>
          <br/><center><input type="submit" name="Submit" value="Submit"/></center>
        </form>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Message">
    <xsl:apply-templates select="key('dataTypes',DataTypeName)">
      <xsl:with-param name="field" select="Name"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="Structure">
    <xsl:param name="field"/>
    <table width="100%" border="0" cellspacing="1" cellpadding="1">
      <tbody>
        <xsl:for-each select="Members/Member">
          <tr>
            <td valign="top">
              <xsl:value-of select="Name"/>
            </td>
            <td>
              <xsl:apply-templates select="key('dataTypes',DataTypeName)">
                <xsl:with-param name="field" select="concat($field,'_',Name)"/>
              </xsl:apply-templates>
            </td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="*">
    <xsl:param name="field"/>
    <input type="text" name="{$field}" size="30"/>
  </xsl:template>
</xsl:stylesheet>

Discussion

Generating an HTML interface is one of the simplest ways to produce a test-data frontend automatically, but it is not the only one. You might instead create a text-based frontend that prompts for and reads input from stdin. For a more ambitious solution, you could generate a GUI frontend. Avoiding HTML lets you combine this example with the next recipe in a single application. Such a generator may be more complex than these examples, however: the HTML-CGI approach relies on substantial infrastructure already provided by the browser and web server.

An important extension would generate JavaScript or VBScript validation code. Its quality again depends on the repository’s metadata. Consider extending the repository to store minimum and maximum values and/or regular expressions used for validation.

  

 

 

 

back to top