1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/docs/multivalue.html Wed Jul 20 23:41:03 2005 +0000
1.3 @@ -0,0 +1,162 @@
1.4 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1.5 +<html xmlns="http://www.w3.org/1999/xhtml">
1.6 +<head>
1.7 + <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />
1.8 + <title>Creating Applications: Adding Multivalued Fields</title>
1.9 + <meta name="generator"
1.10 + content="amaya 8.1a, see http://www.w3.org/Amaya/" />
1.11 + <link href="styles.css" rel="stylesheet" type="text/css" />
1.12 +</head>
1.13 +<body>
1.14 +<h1>Creating Applications: Adding Multivalued Fields</h1>
1.15 +<p>Although some applications only require multiple-choice fields where
1.16 +only a single value may be chosen, in many situations it is desirable
1.17 +to be able to choose an arbitrary number of values for a particular
1.18 +field. However, up to this point, we have been content to
1.19 +represent form data using a single attribute on a single element to
1.20 +represent any given field value. With multivalued fields, we must
1.21 +choose a different strategy in using XML to represent such information.</p>
1.22 +<p>Let us consider permitting multiple type values to be associated
1.23 +with our items. We revise our <a href="data.html">form data structure</a>
1.24 +to
1.25 +be the following:</p>
1.26 +<pre><?xml version="1.0"?><br /><structure><br /> <item value="some value"><br /> <types><br /> <type value="some type"/><br /> <type value="some other type"/><br /> </types><br /> <subitem subvalue="some other value"/><br /> </item><br /></structure></pre>
1.27 +<h2>Multivalued Fields</h2>
1.28 +<p>We shall now take advantage of those HTML form fields which permit
1.29 +users to select one
1.30 +or many values presented in a list or menu.</p>
1.31 +<form method="post" action="" name="single">
1.32 + <p>Some item: <input name="value" value="some value" /><input
1.33 + name="remove" value="Remove" type="submit" /></p>
1.34 + <p>Item type:
1.35 + <select multiple="multiple" name="type">
1.36 + <option>(Not selected)</option>
1.37 + <option>Important</option>
1.38 + <option>Not important</option>
1.39 + <option>Personal</option>
1.40 + </select>
1.41 + </p>
1.42 + <p>Itself containing more items:</p>
1.43 + <p>Sub-item: <input name="subvalue" value="some other value" /><input
1.44 + name="remove2" value="Remove" type="submit" /></p>
1.45 +</form>
1.46 +From the item type list many value may now be selected.
1.47 +<p>Taking the example HTML code from before, we can add a
1.48 +definition of this new list to the template to produce something
1.49 +like this:</p>
1.50 +<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:this-attribute()}" type="text" value="{$this-value}" /><br /> <input name="remove={template:this-element()}" type="submit" value="Remove" /><br /> </p><br /> <span
1.51 + style="font-weight: bold;"><p template:element="types"></span><br /> Item type:<br /> <select template:element="type" name="<span
1.52 + style="font-weight: bold;">{template:list-attribute('value')}</span>" <span
1.53 + style="font-weight: bold;">multiple="multiple"</span>><br /> <option template:element="type-enum" template:expr="<span
1.54 + style="font-weight: bold;">@value-is-set</span>" template:expr-attr="selected"<br /> template:value="@value" value="{@value}" /><br /> </select><br /> </p><br /> <p><br /> Itself containing more items:<br /> </p><br /> <p template:element="subitem"><br /> Sub-item: <input template:attribute="subvalue" name="{template:this-attribute()}" type="text" value="{$this-value}" /><br /> <input name="remove2={template:this-element()}" type="submit" value="Remove" /><br /> </p><br /> <p><br /> <input name="add2={template:this-element()}" type="submit" value="Add subitem" /><br /> </p><br /></div><br /><p><br /> <input name="add={template:this-element()}" type="submit" value="Add item" /><br /></p><span
1.55 + style="font-weight: bold;"><br /><br /></span><!-- Template text between the interesting part and the end. --><br /><br /></form><br /></body><br /></html></pre>
1.56 +<p>From the previous <a href="mulitple.html">single-valued case</a>,
1.57 +some crucial changes have been made:</p>
1.58 +<ol>
1.59 + <li>The paragraph surrounding the field has been annotated to map
1.60 +onto the <code>types</code> element in the form data structure.</li>
1.61 + <li>The <code>select</code> element remains mapped onto the <code>type</code>
1.62 +element in the form data structure. However, we indicate in the name of
1.63 +the <code>select</code>
1.64 +element that the value submitted maps onto a special kind of attribute.
1.65 +Instead of mapping onto a single attribute on a single element, the
1.66 +value maps onto a single attribute on a single element <span
1.67 + style="font-style: italic;">for each value submitted</span>. So for
1.68 +each value selected in the list or menu, a <code>type</code>
1.69 +element is created with the <code>value</code>
1.70 +attribute containing that value.</li>
1.71 + <li>Of course, the <code>select</code> element now has a <code>multiple</code>
1.72 +attribute defined to permit multiple value selections.</li>
1.73 + <li>Inside the <code>select</code> element, the <code>option</code>
1.74 +element mapping onto
1.75 +a <code>type-enum</code> element using a different <code>template:expr</code>
1.76 +condition than was used before.</li>
1.77 +</ol>
1.78 +<h2>Output Structures</h2>
1.79 +<p>Just as in the single-valued case, the revised the form data
1.80 +structure for input does not quite match the structure used by the
1.81 +template. Therefore, we shall define an output form data structure as
1.82 +follows:</p>
1.83 +<pre><?xml version="1.0"?><br /><structure><br /> <item value="some value"><br /> <types><br /> <type><br /> <type-enum value="some type"/><br /> <type-enum value="some other type"/><br /> </type><br /> </types><br /> <subitem subvalue="some other value"/><br /> </item><br /></structure></pre>
1.84 +<p>It is worth noting that the above structure does not define whether
1.85 +many <code>type</code> elements will exist within each <code>types</code>
1.86 +element or whether another mechanism will be used to specify multiple
1.87 +values for each type field.<br />
1.88 +</p>
1.89 +<h3>Presenting the Extra Values</h3>
1.90 +<p>In most respects, the presentation of the extra values is the same
1.91 +as in the single-valued case. The result of the presentation of the
1.92 +extra values is that the <code>types</code> element in the
1.93 +this example structure fragment...</p>
1.94 +<pre><types><br /> <type><br /> <type-enum value="1"/><br /> <type-enum value="2" value-is-set="true"/><br /> <type-enum value="3" value-is-set="true"/><br /> </type><br /></types></pre>
1.95 +<p>...is transformed into something resembling this HTML code:</p>
1.96 +<pre><p><br /> <select name="..." multiple="multiple"><br /> <option value="1">1</option><br /> <option value="2" selected="selected">2</option><br /> <option value="3" selected="selected">3</option><br /> </select><br /></p></pre>
1.97 +<p>Numerous issues arise when considering the above transformation:</p>
1.98 +<ul>
1.99 + <li>Were we not supposed to have many <code>type</code> elements?</li>
1.100 + <li>Where does the special <code>value-is-set</code> attribute
1.101 +come from?</li>
1.102 +</ul>
1.103 +Here, we have made a strategic decision: with the template as defined
1.104 +above, many <code>type</code> elements would have produced
1.105 +many <code>select</code> elements in the Web form, yet this is not
1.106 +what we want; we need to restrict the number of <code>select</code>
1.107 +elements to one per <code>types</code> element whilst marking the
1.108 +selected values so that they may be displayed appropriately in the list
1.109 +or menu.
1.110 +<h2>Merging and Collecting Values</h2>
1.111 +<p>As in the single-valued case, we need to insert the permitted values
1.112 +into the form data so that the template may visit the <code>type-enum</code>
1.113 +elements and extract those values. However, we have now introduced
1.114 +another task to this activity: to collect the selected values together
1.115 +and to produce a unified <code>type</code> element within
1.116 +each <code>types</code> element. In other words, we want to turn
1.117 +something like this...</p>
1.118 +<pre><types><br /> <type value="2"/><br /> <type value="3"/><br /></types></pre>
1.119 +<p>...into something like this:</p>
1.120 +<pre><types><br /> <type><br /> <type-enum value="1"/><br /> <type-enum value="2" value-is-set="true"/><br /> <type-enum value="3" value-is-set="true"/><br /> </type><br /></types></pre>
1.121 +Using the same document containing all the permitted values as our
1.122 +source of information to be merged into the form data, we can now
1.123 +develop a stylesheet which performs the above transformation; this
1.124 +stylesheet needs to work on the
1.125 +following principles:
1.126 +<ol>
1.127 + <li>Descend into the form data structure, copying all elements,
1.128 +attributes and text that the stylesheet is not programmed to recognise.</li>
1.129 + <li>When encountering an <code>item</code> element (which the
1.130 +stylesheet is programmed to recognise), do the following:<br />
1.131 + <ol>
1.132 + <li>Copy the element "skeleton" and its attributes so that
1.133 +the <code>value</code> attribute is retained.</li>
1.134 + <li>Produce a new <code>types</code> element and process it.</li>
1.135 + </ol>
1.136 + </li>
1.137 + <li>When processing a new <code>types</code> element, do the
1.138 +following:<br />
1.139 + <ol>
1.140 + <li>Add a single <code>type</code> element within it.</li>
1.141 + <li>Inside this new <code>type</code> element, add the <code>type-enum</code>
1.142 +elements from the
1.143 +document containing the values, and if any <code>type</code>
1.144 +elements were found within the <code>item</code> element, specify
1.145 +these for the activity.</li>
1.146 + </ol>
1.147 + </li>
1.148 + <li>When adding the <code>type-enum</code> elements, if any of
1.149 +them have a <code>value</code> attribute which matches any of the <code>value</code>
1.150 +attributes of the found <code>type</code> elements, set the special
1.151 + <code>value-is-set</code> attribute on that <code>type-enum</code>
1.152 +element.</li>
1.153 +</ol>
1.154 +<p>The stylesheet source code can be found in <code>examples/Common/VerySimple/Resources/structure_multivalue_types.xsl</code>,
1.155 +whereas the document defined above which contains the values can be
1.156 +found in <code>examples/Common/VerySimple/Resources/structure_types.xml</code>.</p>
1.157 +<h3>Updating the Web Resource</h3>
1.158 +<p>To update the special WebStack resource, we
1.159 +now need to modify a few of the class attributes and to add a few
1.160 +others:</p>
1.161 +<pre> template_resources = {<br /> "structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl")<br /> }<br /> transform_resources = {<br /> "types" : ["structure_multivalue_types.xsl"]<br /> }<br /></pre>
1.162 +<p>With these adjustments, it should now be possible to manipulate the
1.163 +items and subitems whilst specifying multiple type values on each item.</p>
1.164 +</body>
1.165 +</html>