1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 5 <title>Creating Applications: Adding Multiple-Choice Fields and Values</title> 6 <meta name="generator" 7 content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 8 <link href="styles.css" rel="stylesheet" type="text/css" /> 9 </head> 10 <body> 11 <h1>Creating Applications: Adding Multiple-Choice Fields and Values</h1> 12 <p>Up to this point, we have only considered two kinds of Web form 13 fields: text entry fields and action buttons. Since most Web forms 14 offer more convenient ways of entering certain kinds of data, we shall 15 now investigate multiple-choice fields as an example of how XSLForms 16 handles more complicated types of fields.</p> 17 <p>We shall revise our <a href="data.html">form data structure</a> to 18 be the following:</p> 19 <pre><?xml version="1.0"?><br /><structure><br /> <item value="some value"><br /> <type value="some type"/><br /> <subitem subvalue="some other value"/><br /> </item><br /></structure></pre> 20 <h2>Single-Valued Fields</h2> 21 <p>Whilst HTML offers types of form fields where users can select one 22 or many values presented in a list or menu, we shall first consider the 23 case where only a single value can be chosen from such a selection.</p> 24 <form Method="POST" Action="none" Name="single"> 25 <p>Some item: <input name="value" value="some value" /><input 26 name="remove" value="Remove" type="submit" /></p> 27 <p>Item type: 28 <select name="type"> 29 <option>(Not selected)</option> 30 <option>Important</option> 31 <option>Not important</option> 32 <option>Personal</option> 33 </select> 34 </p> 35 <p>Itself containing more items:</p> 36 <p>Sub-item: <input name="subvalue" value="some other value" /><input 37 name="remove2" value="Remove" type="submit" /></p> 38 </form> 39 From the item type list only one value may be selected. 40 <p>Taking the example HTML code from before, we can add a 41 definition of this new list to the template to produce something 42 like this:</p> 43 <pre><html xmlns="http://www.w3.org/1999/xhtml"<br /> xmlns:template="http://www.boddie.org.uk/ns/xmltools/template"><br /><head><br /> <title>Example</title><br /></head><br /><body template:element="structure"><br /><form action="" method="POST"><br /><br /><!-- Template text between the start and the interesting part. --><br /><br /><div template:element="item"><br /> <p><br /> Some item: <input template:attribute="value" name="{template:field-name()}" type="text" value="{$this-value}" /><br /> <input name="remove={template:this-position()}" type="submit" value="Remove" /><br /> </p><br /> <span 44 style="font-weight: bold;"><p></span><br 45 style="font-weight: bold;" /><span style="font-weight: bold;"> Item type:</span><br 46 style="font-weight: bold;" /><span style="font-weight: bold;"> <select template:element="type" name="{template:new-field('value')}"></span><br 47 style="font-weight: bold;" /><span style="font-weight: bold;"> <option template:element="type-enum" template:expr="@value-is-set" template:expr-attr="selected"</span><br 48 style="font-weight: bold;" /><span style="font-weight: bold;"> template:value="@value" value="{@value}"></option></span><br 49 style="font-weight: bold;" /><span style="font-weight: bold;"> </select></span><br 50 style="font-weight: bold;" /><span style="font-weight: bold;"> </p></span><br /> <p><br /> Itself containing more items:<br /> </p><br /> <p template:element="subitem"><br /> Sub-item: <input template:attribute="subvalue" name="{template:field-name()}" type="text" value="{$this-value}" /><br /> <input name="remove2={template:this-position()}" type="submit" value="Remove" /><br /> </p><br /> <p><br /> <input name="add2={template:this-position()}" type="submit" value="Add subitem" /><br /> </p><br /></div><br /><p><br /> <input name="add={template:this-position()}" type="submit" value="Add item" /><br /></p><span 51 style="font-weight: bold;"><br /><br /></span><!-- Template text between the interesting part and the end. --><br /><br /></form><br /></body><br /></html></pre> 52 <p>There are a lot of details here that need to be explained. Here is 53 what was done:</p> 54 <ol> 55 <li>A paragraph was added to provide some space on the page for the 56 field.</li> 57 <li>Inside the paragraph, next to the label text, an HTML <code>select</code> 58 element was added.</li> 59 <li>The <code>select</code> element is mapped onto the <code>type</code> 60 element in the form data structure. However, HTML fields must produce 61 values and it makes no sense to interpret a textual value as an 62 element. Therefore, we indicate in the name of the <code>select</code> 63 element that the value submitted maps onto the <code>value</code> 64 attribute of the <code>type</code> element in the form data 65 structure.</li> 66 <li>Inside the <code>select</code> element, we include an <code>option</code> 67 element which defines the values which will be presented to users 68 of the form. Note that the <code>option</code> element maps onto 69 a <code>type-enum</code> element which is not mentioned in our 70 revised form data structure above; this will be discussed below.</li> 71 </ol> 72 <h2>Output Structures</h2> 73 <p>Although we revised the form data structure above, and whilst the 74 revised structure can describe form data submitted by users of our 75 application, it is unfortunately not sufficient to define the form data 76 that is to be presented. Consider the multiple-choice values that shall 77 be presented to users: such values are not defined in our revised 78 structure. Therefore, we shall define an output form data structure as 79 follows:</p> 80 <pre><?xml version="1.0"?><br /><structure><br /> <item value="some value"><br /> <type value="some type"><br /> <type-enum value="choice #n"/><br /> </type><br /> <subitem subvalue="some other value"/><br /> </item><br /></structure></pre> 81 <p>But since we will receive a structure resembling that defined 82 earlier, yet need to present a structure like the one above, we will 83 need to find a way of merging the range of allowed values into the 84 user-edited form data before presenting that data using our template.</p> 85 </body> 86 </html>