In this final example we show two user input fields
for integers a and
b. After the submit of the form, the integers
are added. This is done using a Monet query.
<article xmlns:c='jelly:core'
xmlns:monet='http://monet.nag.co.uk/monet/ns'
xmlns:om='http://www.openmath.org/OpenMath'
xmlns:x='jelly:xml'
xmlns:xforms='http://www.w3.org/2002/xforms'>
<title>XForms Example</title>
<x:parse var='default_aa'>
<om:OMOBJ>
<om:OMI>1</om:OMI>
</om:OMOBJ>
</x:parse>
<c:set var='default_a' trim='true'>
<om:OMOBJ>
<om:OMI>1</om:OMI>
</om:OMOBJ>
</c:set>
<c:set var='default_b' trim='true'>
<om:OMOBJ>
<om:OMI>2</om:OMI>
</om:OMOBJ>
</c:set>
<x:if select='not(formdata)'>
<x:parse var='formdata'>
<variables>
<a>
<c:out value=''/>
</a>
<b>
<c:out value=''/>
</b>
</variables>
</x:parse>
</x:if>
<!--
Copy the formdata (default or submitted)
-->
<xforms:model>
<xforms:instance>
<x:copyOf select='formdata/*'/>
</xforms:instance>
</xforms:model>
<para>
<xforms:textarea ref='a' class='math-editor'>
<xforms:label>integer a:</xforms:label>
</xforms:textarea>
<xforms:textarea ref='b' class='math-editor'>
<xforms:label>integer b:</xforms:label>
</xforms:textarea>
<xforms:submit submission='submission'>
<xforms:label>submit</xforms:label>
</xforms:submit>
</para>
<!--
convert the form variables to xml
note, this will give an error if the input is not xml
exceptions can be caught with c:catch
-->
<c:set var='a_text' encode='false'>
<x:expr select='formdata/*/a'/>
</c:set>
<x:parse var='a_xml' text=''/>
<c:set var='b_text' encode='false'>
<x:expr select='formdata/*/b'/>
</c:set>
<x:parse var='b_xml' text=''/>
<!-- calculate a+b and place the result in aplusb -->
<x:parse var='aplusb'>
<monet:query>
<monet:classification>
<monet:directive-type href='http://mathdox.org/phrasebook/default#eval'/>
</monet:classification>
<monet:body>
<monet:output>
<om:OMOBJ>
<om:OMA>
<om:OMS cd='arith1' name='plus'/>
<x:copyOf select='a_xml/om:OMOBJ/*'/>
<x:copyOf select='b_xml/om:OMOBJ/*'/>
</om:OMA>
</om:OMOBJ>
</monet:output>
</monet:body>
</monet:query>
</x:parse>
<para>
<om:OMOBJ>
<om:OMV name='a'/>
</om:OMOBJ>
: <x:copyOf select='a_xml/*'/>
</para>
<para>
<om:OMOBJ>
<om:OMV name='b'/>
</om:OMOBJ>
: <x:copyOf select='b_xml/*'/>
</para>
<para>
<om:OMOBJ>
<om:OMA>
<om:OMS cd='arith1' name='plus'/>
<om:OMV name='a'/>
<om:OMV name='b'/>
</om:OMA>
</om:OMOBJ>
:
<om:OMOBJ>
<om:OMA>
<om:OMS cd='relation1' name='eq'/>
<om:OMA>
<om:OMS cd='arith1' name='plus'/>
<x:copyOf select='a_xml/om:OMOBJ/*'/>
<x:copyOf select='b_xml/om:OMOBJ/*'/>
</om:OMA>
<x:copyOf select='aplusb/*/*'/>
</om:OMA>
</om:OMOBJ>
</para>
</article>
This example has roughly the same structure as the above. However, in the handling of the variables we some difference.
The intial values of the variables are stored as strings
in the Jelly c-variable
default_a
and
default_b
.
These strings are then written inside the XML variable
formaldata
.
To use these variables as XML again, one writes
their value in a string-variable
like
<c:set var='a_text' encode='false'>
<x:expr select='$formdata/*/a'/>
</c:set>
and then parses them to XML by
<x:parse var='a_xml' text=''/>
Now the variables are ready to be used inside the Monet query:
<monet:query>
<monet:classification>
<monet:directive-type href='http://mathdox.org/phrasebook/default#eval'/>
</monet:classification>
<monet:body>
<monet:output>
<om:OMOBJ>
<om:OMA>
<om:OMS cd='arith1' name='plus'/>
<x:copyOf select='a_xml/om:OMOBJ/*'/>
<x:copyOf select='b_xml/om:OMOBJ/*'/>
</om:OMA>
</om:OMOBJ>
</monet:output>
</monet:body>
</monet:query>
See the example working (only if the maxima service is set up properly).