<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>Form. And Content</title>
  <link rel="stylesheet" href="projection.css" />
  <link rel="alternate stylesheet" title="Overview" href="overview.css" />
  <meta name="generator" content="amaya 9.4, see http://www.w3.org/Amaya/" />
  <style type="text/css">
.source {text-align: right}
iframe {width: 90%; height: 40ex;}</style>
</head>

<body>

<div>
<h1>Form. And Content</h1>

<p><a href="http://www.cwi.nl/~steven/">Steven Pemberton</a>, <a
href="http://www.cwi.nl/">CWI</a>, <a
href="http://www.cwi.nl/~steven/amsterdam.html">Amsterdam</a></p>

<p><img src="hamleeet.jpg" alt="The author" /></p>
</div>

<div>
<h2>New Technologies</h2>

<p>Through the ages, whenever new technologies are introduced, as often as not
they are quite unnecessarily made to look like the thing they are replacing,
often to the detriment of users.</p>
</div>

<div>
<h2>Books</h2>

<p><img alt="A printed book imitating a manuscript"
src="chroniclesofengland1485.jpg" /> </p>
</div>

<div>
<h2>Cars</h2>

<p><img alt="An early car" src="carriage.jpg" /></p>
</div>

<div>
<h2>Apps</h2>

<p><img alt="A diary app" src="psion5-agenda.gif" /></p>
</div>

<div>
<h2>Apps</h2>

<p><img alt="An app" src="compressor.png" /></p>
</div>

<div>
<h2>Digital Watches</h2>

<p><img alt="A digital watch" src="watch.png" /></p>
</div>

<div>
<h2>Electronic Forms</h2>

<p><img alt="e-Taxform" src="taxform.jpg" width="620" height="349" />Forms also
suffer under their legacy of paper.</p>

<p>Early web forms were often required to be <em>identical</em> to their paper
versions, thus negating some of the advantages of electronic forms.</p>
</div>

<div>
<h2>What is a Form?</h2>

<p>"You know it when you see it"</p>

<p>"Rectangular boxes you can type in"</p>

<p>Is a Dialogue box a form?</p>

<p>How about when you are asked to click on a location in a map?</p>
</div>

<div>
<h2>This is a form</h2>

<p><img alt="A skeumorphic app" src="compressor.png" /></p>
</div>

<div>
<h2>Form-driven data</h2>

<p>Classically, a form drives the data:</p>

<blockquote>
  The controls on the form dictate the structure of the underlying data being
  collected. </blockquote>
</div>

<div>
<h2>Data-driven forms</h2>

<p>The data drives the form, and not vice versa:</p>

<blockquote>
  The structure of the underlying data causes the controls to emerge.
</blockquote>

<p>The form is no longer static, but dynamically reflects the data.</p>

<p>Three examples:</p>
<ul>
  <li>Structure</li>
  <li>Presentation</li>
  <li>Execution</li>
</ul>
</div>

<div>
<h2>XForms, a reminder</h2>

<p>Separation of data from controls.</p>

<p>Similar to separation of style from content with CSS, with similar
advantages.</p>

<p>Instances of data + properties.</p>
<pre>&lt;instance src="sale.xml"/&gt;
&lt;bind ref="..." <em>property</em>/&gt;</pre>

<p>Example property: Relevance</p>
<pre>&lt;bind ref="cc-number" relevant="../payment-method = 'credit'"/&gt;</pre>

<p>Controls in the UI then bind to data nodes, inheriting their properties.</p>
<pre>&lt;input ref="cc-number" label="Credit card number"/&gt;</pre>

<p></p>
</div>

<div>
<h2>Relevance</h2>

<p>Here is an example (try it). If you select Credit card as payment type, the
controls for that appear.</p>

<iframe src="../../../forms/examples/credit-card.xhtml">
</iframe>
<p class="source"><a href="../../../forms/examples/credit-card.xhtml">Source</a></p>
</div>

<div>
<h2>Three examples</h2>
<ul>
  <li>Structure</li>
  <li>Presentation</li>
  <li>Execution</li>
</ul>
</div>

<div>
<h2>Data-driven Structure: A Questionnaire</h2>

<p>A classic form.</p>

<p>Written for an internet community.</p>
</div>

<div>
<h2>Form</h2>

<p>Simplified from the original, this shows the essence (it is live, you can
try it).</p>

<iframe src="../../../forms/examples/data-driven2/form.xml">
</iframe>
<p class="source"><a href="../../../forms/examples/data-driven2/form.xml">Source</a></p>
</div>

<div>
<h2>Classic structure</h2>

<p>The classical markup would look like this:</p>
<pre>Please help us discover problems and solutions
that would improve our processes.

&lt;<strong>select1 ref="choice"</strong>&gt;
   &lt;label&gt;<strong>How can you help?</strong>&lt;/label&gt;
   &lt;item&gt;&lt;label&gt;<strong>You know a problem that needs to be fixed</strong>&lt;/label&gt;
         &lt;value&gt;problem&lt;/value&gt;&lt;/item&gt;
   &lt;item&gt;&lt;label&gt;<strong>You know a 'solution' that doesn't work</strong>&lt;/label&gt;
         &lt;value&gt;failure&lt;/value&gt;&lt;/item&gt;
   &lt;item&gt;&lt;label&gt;<strong>You have a prediction about a future possible failure</strong>&lt;/label&gt;
         &lt;value&gt;prediction&lt;/value&gt;&lt;/item&gt;
&lt;/select1&gt;</pre>
</div>

<div>
<h2>Cases</h2>

<p>One a choice has been made, the questions for that choice are displayed: </p>
<pre>&lt;<strong>switch ref="choice"</strong>&gt;

   &lt;<strong>case name=""</strong>/&gt; &lt;!-- Until a choice is made, nothing is displayed --&gt;

   &lt;<strong>case name="problem"</strong>&gt;
      &lt;group ref="problem"&gt;
         &lt;label&gt;<strong>You know a problem that needs to be fixed</strong>&lt;/label&gt;
         &lt;textarea ref="problem"&gt;&lt;label&gt;What problem do you see?&lt;/label&gt;&lt;/textarea&gt;
         &lt;textarea ref="solution"&gt;&lt;label&gt;Can you propose a solution?&lt;/label&gt;&lt;/textarea&gt;
      &lt;/group&gt;
   &lt;/case&gt;

   &lt;<strong>case name="failure"</strong>&gt;
      ... etc ...</pre>
</div>

<div>
<h2>Data</h2>

<p>The data being produced would look like this:</p>
<pre>&lt;data&gt;
   &lt;choice&gt;problem&lt;/choice&gt;
   &lt;<strong>problem</strong>&gt;
      &lt;problem&gt;Too slow!&lt;/problem&gt;
      &lt;solution&gt;Bigger pipes&lt;/solution&gt;
   &lt;/problem&gt;
   &lt;<strong>failure</strong>&gt;
      &lt;problem/&gt;
      &lt;solution/&gt;
   &lt;/failure&gt;
   &lt;<strong>prediction</strong>&gt;
      &lt;problem/&gt;
      &lt;likely/&gt;
      &lt;who/&gt;
      &lt;solution/&gt;
   &lt;/prediction&gt;
&lt;/data&gt;</pre>
</div>

<div>
<h2>Multilingual</h2>

<p>Making it multilingual was easy. Instead of literal labels, they get their
text from an instance:</p>
<pre>&lt;group ref="problem"&gt;
    &lt;label&gt;<strong>You know a problem that needs to be fixed</strong>&lt;/label&gt;
    &lt;textarea ref="problem"&gt;&lt;label&gt;What problem do you see?&lt;/label&gt;&lt;/textarea&gt;
    &lt;textarea ref="solution"&gt;&lt;label&gt;Can you propose a solution?&lt;/label&gt;&lt;/textarea&gt;
&lt;/group&gt;</pre>

<p>⇒</p>
<pre>&lt;group ref="problem"&gt;
   &lt;label <strong>ref="instance('m')/problem/label"</strong>/&gt;
   &lt;textarea ref="problem"&gt; &lt;label ref="instance('m')/problem/problem"/&gt;&lt;/textarea&gt;
   &lt;textarea ref="solution"&gt;&lt;label ref="instance('m')/problem/solution"/&gt;&lt;/textarea&gt;
&lt;/group&gt;</pre>

<p>Once we had made this change, we realised that the three cases were almost
identical.</p>
</div>

<div>
<h2>Replace</h2>

<p>We could replace the whole switch with three options with a single group,
essentially a single option that adpats to the data:</p>
<pre>&lt;switch ref="choice"&gt;
   &lt;case name=""/&gt;
   &lt;case name="problem"&gt;... problem, solution
   &lt;case name="failure"&gt;... problem, solution
   &lt;case name="prediction"&gt;... problem, likely, who, solution
&lt;/switch&gt;</pre>

<p>⇒</p>
<pre>&lt;group ref="<strong>answer[@choice=../choice]</strong>"&gt;
   &lt;label ref="instance('m')/answer[@choice=context()/@choice]/label"/&gt;
   &lt;textarea ref="<strong>problem</strong>"&gt;...&lt;/textarea&gt;
   &lt;select1 ref="<strong>likely</strong>"&gt;...&lt;/select1&gt;
   &lt;textarea ref="<strong>who</strong>"&gt;...&lt;/textarea&gt;
   &lt;textarea ref="<strong>solution</strong>"&gt;...&lt;/textarea&gt;
&lt;/group&gt;</pre>

<p>In both cases, initially no answer has a selected choice: nothing will be
displayed for the answers.</p>

<p>If the data for the selected answer doesn't have a <code>likely</code> or
<code>who</code> element, the controls for those elements won't be
displayed.</p>
</div>

<div>
<h2>New data</h2>

<p>Slight change to the data to support this:</p>
<pre>&lt;data&gt;
   &lt;choice/&gt;
   &lt;<strong>answer choice="problem"</strong>&gt;
      &lt;problem/&gt;
      &lt;solution/&gt;
   &lt;/answer&gt;
   &lt;<strong>answer choice="failure"</strong>&gt;
      &lt;problem/&gt;
      &lt;solution/&gt;
   &lt;/answer&gt;
   &lt;<strong>answer choice="prediction"</strong>&gt;
      &lt;problem/&gt;
      &lt;likely/&gt;
      &lt;who/&gt;
      &lt;solution/&gt;
   &lt;/answer&gt;
&lt;/data&gt;</pre>
</div>

<div>
<h2>Harvesting choices</h2>

<p>An advantage is that the form is no longer bound by the number of choices;
the select just harvests that from the data:</p>
<pre>&lt;<strong>select1 ref="choice"</strong>&gt;
   &lt;label&gt;<strong>How can you help?</strong>&lt;/label&gt;
   &lt;item&gt;&lt;label&gt;<strong>You know a problem that needs to be fixed</strong>&lt;/label&gt;
         &lt;value&gt;problem&lt;/value&gt;&lt;/item&gt;
   &lt;item&gt;&lt;label&gt;<strong>You know a 'solution' that doesn't work</strong>&lt;/label&gt;
         &lt;value&gt;failure&lt;/value&gt;&lt;/item&gt;
   &lt;item&gt;&lt;label&gt;<strong>You have a prediction about a future possible failure</strong>&lt;/label&gt;
         &lt;value&gt;prediction&lt;/value&gt;&lt;/item&gt;
&lt;/select1&gt;</pre>

<p>⇒</p>
<pre>&lt;select1 <strong>ref="choice"</strong>&gt;
 &lt;label ref="instance('m')/choice/label"/&gt;
   &lt;itemset ref="<strong>instance('m')/choice/item</strong>"&gt;
       &lt;label ref="."/&gt;
       &lt;value ref="@value"/&gt;
   &lt;/itemset&gt;
&lt;/select1&gt;</pre>
</div>

<div>
<h2>Analysis</h2>

<p>The form is now driven from two data files: the template for the data, and
the messages. </p>

<p>Essentially, the group of controls acts as an interpreter of the data.</p>

<p>For instance, add this to the data:</p>
<pre>&lt;answer choice="solution"&gt;
   &lt;problem/&gt;
   &lt;who/&gt;
   &lt;solution/&gt;
&lt;/answer&gt;</pre>

<p>along with suitable messages, and the form works <em>without change</em>
with the new entry.</p>

<p>Good for content providers.</p>
</div>

<div>
<h2>Data-driven Display: A Presentation Manager</h2>

<p>CSS presentation mode allows a different set of rules to apply when
projecting on a screen.</p>
<pre>@media projection {
 ...
}</pre>

<p>Advantages:</p>
<ul>
  <li>the content is easily repurposable</li>
  <li>choice of editing software,</li>
  <li>platform independent,</li>
  <li>a lot of control,</li>
  <li>guarantees a long life for the content: proprietary software has the risk
    of the content no longer being readable after several years.</li>
</ul>

<p>Alas, CSS presentation mode is now effectively dead.</p>
</div>

<div>
<h2>New presentations</h2>

<p>This has caused the emergence of <em>dozens</em> of Javascript packages to
do the same job, such as reveal, remark, webslides, deck, shwr, ...</p>

<p>Disadvantages:</p>
<ul>
  <li>Not standardised: you can't swap between packages;</li>
  <li>To repurpose content, you have to edit the content files;</li>
  <li>If support for the package disappears (which has happened), you are in
    trouble: this is like using proprietary software.</li>
</ul>
</div>

<div>
<h2>Solution: Data-driven form</h2>

<p>Very easy: it is a surprisingly small amount of markup;</p>

<p>Allows the continued use and repurposing of existing content without
change;</p>

<p>Continues to give the power of XHTML+CSS for styling.</p>
</div>

<div>
<h2>Slides</h2>

<p>Get the slides from some source, and create a bind that identifies what a
slide is:</p>
<pre>&lt;instance id="slides" 
          src="http://www.cwi.nl/~steven/Talks/2018/prague/"/&gt;
&lt;bind id="slide" ref="<strong>h:body/h:div</strong>[position()=instance('i')/index]"/&gt;</pre>

<p>Using an admin instance:</p>
<pre>&lt;instance id="i"&gt;
    &lt;admin xmlns=""&gt;
        <strong>&lt;index&gt;1&lt;/index&gt;</strong>
    &lt;/admin&gt;
&lt;/instance&gt;</pre>

<p>Display one slide:</p>
<pre>&lt;group bind="slide"&gt;
   ...
&lt;/group&gt;</pre>
</div>

<div>
<h2>Controls</h2>

<p>Slide clicker remotes act like keyboards, and send the characters <code>Page
Up</code> and <code>Page Down</code>:</p>
<pre>&lt;action <strong>ev:event="keydown"</strong> ev:defaultAction="cancel"&gt;
    &lt;setvalue ref="instance('i')/index"
              if="event('key')='PageUp' or
                  event('key')='ArrowLeft'"
              value=". - 1"/&gt;
    &lt;setvalue ref="instance('i')/index"
              if="event('key')='PageDown' or
                  event('key')='ArrowRight'"
              value=". + 1"/&gt;
&lt;/action&gt;</pre>
</div>

<div>
<h2>One slide</h2>
<pre>&lt;group bind="slide"&gt;
   &lt;<strong>repeat ref="*"</strong>&gt;
      &lt;output class="<strong>h1</strong>"  ref=".[name(.)='<strong>h1</strong>']"/&gt;
      &lt;output class="<strong>h2</strong>"  ref=".[name(.)='<strong>h2</strong>']"/&gt;
      &lt;output class="<strong>pre</strong>" ref=".[name(.)='<strong>pre</strong>']"/&gt;
      &lt;group  class="<strong>p</strong>" ref=".[name(.)='<strong>p</strong>'"&gt;...
      ...
   &lt;/<strong>repeat</strong>&gt;
&lt;/group&gt;</pre>

<p>Only one is matched, so only one is displayed.</p>

<p>Shorter:</p>
<pre>&lt;group bind="slide"&gt;
   &lt;<strong>repeat ref="*"</strong>&gt;
      &lt;output class="<strong>{name(.)}</strong>"
              ref=".[name(.)='<strong>h1</strong>' or name(.)='<strong>h2</strong>' or name(.)='<strong>pre</strong>']"/&gt;
      &lt;group  class="<strong>p</strong>"   ref=".[name(.)='<strong>p</strong>'"&gt;...
      ...
   &lt;/<strong>repeat</strong>&gt;
&lt;/group&gt;</pre>

<p></p>
</div>

<div>
<h2>More complicated case</h2>
<pre>&lt;group class="p" ref=".[name(.)='p']"&gt;
    &lt;<strong>repeat ref="node()"</strong>&gt;
        &lt;output class="text"
                ref=".[name(.)='<strong>#text</strong>']"/&gt;
        &lt;output class="{name(.)}"
                ref=".[name(.)='<strong>em</strong>' or
                       name(.)='<strong>strong</strong>' or
                       name(.)='<strong>code</strong>' or
                       name(.)='<strong>a</strong>']"/&gt;
        &lt;output class="img"
                ref=".[name(.)='<strong>img</strong>']"
                value="concat(instance('i')/base,@src)"
                mediatype="image/*"/&gt;
    &lt;/<strong>repeat</strong>&gt;
&lt;/group&gt;</pre>
</div>

<div>
<h2>Analysis</h2>

<p>Such an approach is not a perfect replacement for CSS presentation mode,
<strong>BUT</strong></p>
<ul>
  <li>the content is still easily repurposable</li>
  <li>you still have the choice of editing software,</li>
  <li>it is still platform independent,</li>
  <li>you still have a lot of control,</li>
  <li>it still guarantees a long life for the content.</li>
  <li>It is only around 100 lines of XForms.</li>
  <li>Compared with for instance <em>shower</em>, to take one example, which is
    around <em>ten thousand</em> lines, 100Kbytes of Javascript alone.</li>
</ul>
<p class="source"><a href="slides.xhtml">Source</a></p>
</div>

<div>
<h2>Data-driven Control: The XForms 2.0 Test Suite</h2>

<p>XForms 1 and 1.1 have a test-suite where each feature is tested by a
separate file.</p>
<iframe src="../../../forms/TestSuite/functions/7.7.3.b.xhtml">
</iframe>
<p class="source"><a href="../../../forms/TestSuite/functions/7.7.3.b.xhtml">Source</a></p>

<p>Difficult to maintain, update, and run; requires a lot of user interaction
to see if tests have succeeded.</p>
</div>

<div>
<h2>XForms 2</h2>

<p>Naturally, the XForms 2 test suite is now a self-testing, data-driven
form.</p>
<iframe src="../../../forms/TestSuite/index.xhtml">
</iframe>
<p class="source"><a href="../../../forms/TestSuite/index.xhtml">Source</a></p>

<p>(This is work in progress; the example above is live, but may not always
work).</p>
</div>

<div>
<h2>The individual test cases are data-driven too</h2>
<iframe src="../../../forms/TestSuite/functions/compare.xhtml">
</iframe>
<p class="source"><a href="../../../forms/TestSuite/functions/compare.xhtml">Source</a></p>
</div>

<div>
<h2>Test case</h2>

<p>We want to test that this does the right thing:</p>
<pre>compare('apple', 'orange')</pre>

<p>Enclose it:</p>
<pre><strong>&lt;test&gt;</strong>compare('apple', 'orange')<strong>&lt;/test&gt;</strong></pre>

<p>mark the parameters:</p>
<pre>&lt;test&gt;compare('<strong>&lt;a&gt;</strong>apple<strong>&lt;/a&gt;</strong>', '<strong>&lt;b&gt;</strong>orange<strong>&lt;/b&gt;</strong>')&lt;/test&gt;</pre>

<p>add attributes for the required result, actual result, and whether the test
case passes:</p>
<pre>&lt;test <strong>pass="" res="" req="-1"</strong>&gt;compare('&lt;a&gt;apple&lt;/a&gt;', '&lt;b&gt;orange&lt;/b&gt;')&lt;/test&gt;</pre>
</div>

<div>
<h2>Many test cases, with optional description</h2>
<pre>&lt;instance&gt;
   <strong>&lt;tests pass="" name="compare() function" xmlns=""&gt;</strong>
      &lt;description&gt;...&lt;/description&gt;
      &lt;test pass="" res="" req="-1"&gt;compare(&lt;a&gt;apple&lt;/a&gt;, &lt;b&gt;orange&lt;/b&gt;)&lt;/test&gt;
      &lt;test pass="" res="" req="1"&gt;compare(&lt;a&gt;orange&lt;/a&gt;, &lt;b&gt;apple&lt;/b&gt;)&lt;/test&gt;
      &lt;test pass="" res="" req="0"&gt;compare(&lt;a&gt;apple&lt;/a&gt;, &lt;b&gt;apple&lt;/b&gt;)&lt;/test&gt;
      ...
   <strong>&lt;/tests&gt;</strong>
&lt;/instance&gt;</pre>
</div>

<div>
<h2>Checking</h2>

<p>Test individual results (this is the only line that is different in any test
target, apart from the content of the <code>test</code> instance):</p>
<pre>&lt;bind ref="<strong>test/@res</strong>" calculate="compare(../a, ../b)"/&gt;</pre>

<p>Check whether each passes:</p>
<pre>&lt;bind ref="<strong>test/@pass</strong>" calculate="if(../@res = ../@req, 'yes', 'no')"/&gt;</pre>

<p>Check whether the whole set passes:</p>
<pre>&lt;bind ref="<strong>@pass</strong>" calculate="if(test[@pass!='yes'], 'FAIL', 'PASS')"/&gt;</pre>
</div>

<div>
<h2>Standard controls for all test sets</h2>

<p>These are also the same for each test set:</p>
<pre>&lt;group&gt;
    &lt;label class="title" ref="<strong>@name</strong>"/&gt;
    &lt;output class="block" ref="<strong>description</strong>"/&gt;
    &lt;output <strong>class="{@pass}"</strong> ref="@pass"/&gt;
    &lt;repeat ref="test"&gt;
        &lt;output value="."/&gt; → &lt;output ref="@res"/&gt; 
        &lt;output class="wrong"
                value="<strong>if(@pass!='yes'</strong>, concat(' expected: ', @req), '')"/&gt;
    &lt;/repeat&gt;
&lt;/group&gt;</pre>
</div>

<div>
<h2>Analysis</h2>

<p>Lots of flexibility.</p>

<p>Easy to add new tests.</p>

<p>Much easier to run.</p>

<p>Much easier to tell if a test passes or fails.</p>

<p>(Still work in progress.)</p>
</div>

<div>
<h2>XForms 2: More data driven</h2>

<p>To aid in data-driven forms, the model is being enhanced in XForms 2.0.</p>

<p>Check out the XForms 2.0 spec at <a
href="https://www.w3.org/community/xformsusers/wiki/XForms_2.0">https://www.w3.org/community/xformsusers/wiki/XForms_2.0</a></p>
</div>

<div>
<h2>Conclusion</h2>

<p>Comparison of separating style and content was not accidental.</p>

<p>Data-driven forms add a level of abstraction to our documents, abstracting
<em>essence</em> from the content.</p>

<p>Blurs the distinction between form and application.</p>

<p>A lot of power: allows declarative definition of applications that would
normally be thought of as procedural.</p>

</div>
</body>
</html>
