XML technologies / XSLT / XSLT Cookbook / Transforming XML into HTML / Filling in a form

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to insert XML data into a predefined form before sending it to the client.

Solution

If you use XHTML or otherwise well-formed HTML, XSLT can enrich HTML with XML data. Here, populate a prepared form. Suppose a company designer creates a checkout form requiring sales tax based on the customer’s US state, entered with the billing address. Taxable states and rates vary, so hardcoding them is unwise. The server can insert current tax data dynamically with XSLT.

Sales-tax data might be stored or extracted from a database as follows:

<salesTax>
  <state>
    <name>AL</name>
    <tax>4</tax>
  </state>
  <state>
    <name>AK</name>
    <tax>0</tax>
  </state>
  <state>
    <name>AZ</name>
    <tax>5.6</tax>
  </state>
  ...
  <state>
    <name>WY</name>
    <tax>4</tax>
  </state>
</salesTax>

The prepared HTML form could be:

<html>
  <head>
    <title>Check Out</title>
    <script type="text/javascript" language="JavaScript">
    <!--
    /* Initialisiere Steuersatz für vorgegebenen Bundesstaat */
    setTax(document.customerInfo.billState)
    /* Berechne Steuersatz neu, wenn Bundesstaat sich ändert */
    function setTax(field)
     {
    var value = new String(field.value)
    var commaPos = value.indexOf(",")
    var taxObj = document.customerInfo.tax
    var tax = value.substr(commaPos + 1)
    var subtotalObj = document.customerInfo.subtotal
    taxObj.value = tax
    document.customerInfo.total.value =
      parseFloat(subtotalObj.value) +
      (parseFloat(subtotalObj.value) * parseFloat(tax) / 100.00)
     }
    -->
    </script>
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <h1>Check Out</h1>
    <form name="customerInfo" method="post" action="">
      <table width="70%" border="0" cellspacing="3" cellpadding="3">
        <tr>
          <td width="7%">&#xa0;</td>
          <td width="32%">
            <div align="center"><b>Shipping Address</b></div>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">&#xa0;</td>
          <td width="34%">
            <div align="center"><b>Billing Address</b></div>
          </td>
        </tr>
        <tr>
          <td width="7%">Name</td>
          <td width="32%">
            <input type="text" name="shipName" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Name</td>
          <td width="34%">
            <input type="text" name="billName" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
        </tr>
        <tr>
          <td width="7%">Address</td>
          <td width="32%">
            <input type="text" name="shipAddr" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Address</td>
          <td width="34%">
            <input type="text" name="billAddr" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
        </tr>
        <tr>
          <td width="7%">City</td>
          <td width="32%">
            <input type="text" name="shipCity" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">City</td>
          <td width="34%">
            <input type="text" name="billCity" maxlength="40" size="50" border="0" align="absmiddle"/>
          </td>
        </tr>
        <tr>
          <td width="7%">State</td>
          <td width="32%">
            <select name="shipState" size="1" align="absmiddle"></select>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">State</td>
          <td width="34%">
            <select name="billState" size="1" align="absmiddle" onChange="setTax(this)"></select>
          </td>
        </tr>
        <tr>
          <td width="7%">Zip</td>
          <td width="32%">
            <input type="text" name="shipZip" maxlength="10" size="15" border="0" align="absmiddle"/>
          </td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Zip</td>
          <td width="34%">
            <input type="text" name="billZip" maxlength="10" size="15" border="0" align="absmiddle"/>
          </td>
        </tr>
        <tr>
          <td width="7%">&#xa0;</td>
          <td width="32%">&#xa0;</td>
          <td width="20%">&#xa0;</td>
          <td width="7%">&#xa0;</td>
          <td width="34%">&#xa0;</td>
        </tr>
        <tr>
          <td width="7%">&#xa0;</td>
          <td width="32%">&#xa0;</td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Subtotal</td>
          <td width="34%">
            <input type="text" name="subtotal" readonly="1" value="100.00"/>
          </td>
        </tr>
        <tr>
          <td width="7%">&#xa0;</td>
          <td width="32%">&#xa0;</td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Tax</td>
          <td width="34%">
            <input type="text" name="tax" readonly="1"/>
          </td>
        </tr>
        <tr>
          <td width="7%">&#xa0;</td>
          <td width="32%">&#xa0;</td>
          <td width="20%">&#xa0;</td>
          <td width="7%">Total</td>
          <td width="34%">
            <input type="text" name="total" readonly="1"/>
          </td>
        </tr>
      </table>
    </form>
  </body>
</html>

This transformation is a merge whose default action copies the prepared HTML into output; see recipe Avoiding problems when porting from 1.0 to 2.0. When select elements for shipping and billing states occur, state data is inserted as option elements. For simplicity, both state name and tax rate are included in attribute value on element option, but a JavaScript lookup could be used instead.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="../util/copy.xslt"/>
  <xsl:output method="html"/>
  <xsl:template match="html">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="select[@name='shipState' or @name='billState']">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:for-each select="document('salesTax.xml')/salesTax/state">
        <option value="{name}',',{tax}">
          <xsl:value-of select="name"/>
        </option>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Discussion

Other technologies, perhaps ASP or JSP, might suit this example better. Nevertheless, two aspects may favor an XSLT solution.

First, the prepared HTML is ordinary HTML without embedded server- or client-side scripting. A little client scripting is used, but the transformation could insert it too. The page author need not know the filling technology or have programming skills. Conversely, the transformation programmer needs no HTML or graphic-design expertise, which describes me fairly well.

The two need only agree on content and naming conventions. This separates responsibilities for dynamic content in a way other techniques lack.

Second, transformations can also remove or reorder existing content, without cluttering HTML with foreign syntax.

See also

Time and space prevent covering XForms here, but this new technology deserves attention. The W3C describes XForms as follows:

Current web forms do not separate purpose from presentation. XForms has separate sections describing what a form does and how it looks, allowing flexible presentation, including traditional XHTML forms attached to an XML form definition.

Main XForms goals:

  • Support browsers on PDAs, televisions and desktops, plus printers and scanners

  • Rich interfaces for business and personal applications and device control

  • Separate data, logic and presentation

  • Improved internationalization

  • Support structured form data

  • Advanced form logic

  • Multiple forms per page and multiple pages per form

  • Support suspending and resuming form completion

  • Seamless integration with other XML tag sets

  

 

 

 

back to top