XRX - Restful XForms

Steven Pemberton, CWI Amsterdam

XRX

"I was working on a project with real-estate transactions that had many associated complex real-estate forms. Traditional methods required approximately 40 inserts into separate tables within a relational database. The use of XForms and eXist resulted in one line of XQuery code:

store(collection, file, data)

That was it. Simple. Elegant.

I was hooked. After spending over 20 years building applications with a variety of procedural languages I found my preferred architecture. I have seen the power of XForms and eXist and can't conceive of returning to my procedural programming ways."

XRX: Simple, Elegant, Disruptive (Dan McCreary, O'Relliynet, 2008)

XRX

XForms: a W3C technology, developed at the CWI and elsewhere, originally designed to be an updated and easier-to-produce forms language for the web. Because of its generality, it has evolved into a way of producing applications as well.

Rest: 'Representational State Transfer ' A term defined by Roy Fielding, a principal author of the HTTP spec, for a particular software architecture, such as the Web uses.

XQuery: An XML database technology.

XForms

XForms is a declarative language, where data is kept up-to-date using constraints, in a similar way to how spreadsheets work.

"Declarative" means that a lot of the administration that you normally have to do when programming is done for you by the system.

Example: to do (34 lines)

(Source)

Example: maps (37 lines of mark up)

(Source)

XForms Structure

XForms has a data model, where all data is stored, and in the body controls where data values can be input or output:

<model>
    <instance>
        <data xmlns="">
          <firstname/><surname/>
       </data>
   </instance>
</model>
 ...
<input ref="firstname"><label>First name</label></input>
 ...
<output ref="firstname"/>

Input and output

(Source)

Values can be calculated

<model>
   <instance>
       <data xmlns="">
          <height/><width/><depth/><volume/>
       </data>
   </instance>
   <bind nodeset="volume"
         calculate="../width*../height*../depth"/>
</model>

Volume example

(Source)

Controls are abstract

Here are three identical controls, just styled differently

(Source)

The control

<select1 ref="sex">
    <label>Gender</label>
    <item>
        <label>Male</label>
        <value>M</value>
    </item>
    <item>
        <label>Female</label>
        <value>F</value>
    </item>
</select1>

Data can be obtained from external sources

<model>
    <instance src="http://..."/>
</model>

And data can be written back to external sources.

<submission resource="http://..." .../>

And values can be output anywhere

<input ref="age">
    <label><output ref="age-text"/></label>
</input>

Example

(Source)

Languages file for Dutch

(Source)

Example

(Source)

The language file

(Source)

Maps

<instance>
 <data xmlns="">
   <zoom>10</zoom>
   <posx>87140</posx> <posy>130979</posy>
   <x/><y/><scale/><maxpos/><url/>
 </data>
</instance>

<bind nodeset="scale" calculate="power(2, 18 - ../zoom)"/>
<bind nodeset="maxpos" calculate="power(2, 18)-1"/>
<bind nodeset="x" calculate="floor(../posx div ../scale)"/>
<bind nodeset="y" calculate="floor(../posy div ../scale)"/>
<bind nodeset="url"
  calculate="concat('http://a.tile.openstreetmap.org/',
                  ../zoom, '/' , ../y, '/', ../x,  
                  '.png')"/> 

All the interaction has to do is update zoom, posx, and posy, and output the tile:

<output mediatype="image/*" ref="url" />

Selectors

As you have seen, most selectors ('variable names') are just simple names like

ref="x"

or the occasional

"../y"

This already hints at the fact that selectors in XForms look like directory paths (in fact they are called XPaths). So rather than Javascript

coords.x

you write

coords/x

Example: Interaction

Wikipedia example

Wikipedia

<instance id="isearch">
  <data xmlns="">
    <action>opensearch</action>
    <search/>
  </data>
</instance>

<submission resource="http://en.wikipedia.org/w/api.php"
  method="get" replace="instance" instance="suggestions"
/>
    
<instance id="suggestions"><data xmlns=""/></instance>

This generates the url:

http://en.wikipedia.org/w/api.php?action=opensearch&search=whatever

Results

Wikipedia gives you results that look like this:

<SearchSuggestion version="2.0" xmlns="http://opensearch.org/searchsuggest2">
  <Query>XML</Query>
  <Section>
    <Item>
      <Text>XML</Text>
      <Description>Extensible Markup Language (XML) is a set of rules for encoding documents in machine-readable form. </Description>
      <Url>http://en.wikipedia.org/wiki/XML</Url>
    </Item>
    <Item>
      <Text>XML Schema (W3C)</Text>
      <Description>XML Schema, published as a W3C recommendation in May 2001, is one of several XML schema languages. 
      </Description>
      <Url>http://en.wikipedia.org/wiki/XML_Schema_(W3C)</Url>
    </Item>
    ...

Controls

We bind to the search string:

<input ref="search" incremental="true" />

And then, every time the value changes we submit the data:

<send ev:event="xforms-value-changed"/>

We then display the results with:

<repeat nodeset="instance('suggestions')/Section/Item">
     <output value="Text"/>
</repeat>

XRX

The reason the Dan McCreary got so excited in the first slide is that you can populate an instance with data from a database using a single query, update it within XForms, and then pop it back into the database with a single submit.

But it doesn't have to be a database, it can be anything the server serves, a python program, a script, or whatever. That's an advantage of the REST architecture: server and client can be completely independent.

The cost of producing applications

According to the DoD, 90% of the cost of software is debugging.

According to Fred Brookes, in his classic book The Mythical Man Month, the number of bugs increases quadratically according to code size: L1.5.

In other words, a program that is 10 times longer is 32 times harder to write.

Or put another way: a program that is 10 times smaller needs only 3% of the effort.

Constructing Applications

The problem is, no one writes applications except programmers.

Interesting exception: spreadsheets

Mostly because they use a declarative programming model.

The nice part about declarative programming is that the computer takes care of all the boring fiddly detail.

Data point: Big machines

A certain company makes BIG machines (walk in): user interface is very demanding — needs 5 years, 30 people.

This became: 1 year, 10 people with XForms.

Do the sums. Assume one person costs 100k a year. Then this has gone from a 15M cost to a 1M cost. They have saved 14 million! (And 4 years)

Data point: applets

A company is producing many small applications using XForms, based on previous Javascript versions.

They report that the code is less than 25% of the original size (so a tenth of the work).

"[The designers] are really happy to not have to use javascript by the way: they like that if things don't work its not their fault"

Data point: application

(True story)

Manager: I want you to come back to me in 2 days with estimates of how long it will take your teams to make the application

[2 days later]

Javascript man: I'll need 30 days to work out how long it will take to program it

XForms man: I've already done it.

Implementations

There are many implementations of XForms:

And there are many types of implementation:

Conclusion

Declarative applications require much less work to produce

Experience has shown that an application requires about an order of magnitude less work to produce in XForms.

XRX makes database manipulation very much easier.

More on XForms: XForms 1.1 Tutorial