XForms & Cause-and-Effect Programming 

by John M. Boyer





Listing One



<xforms:model

       xmlns:xforms="http://www.w3.org/2002/xforms"

       xmlns:xsd="http://www.w3.org/2001/XMLSchema"

       schema="loan.xsd" functions="power">

       <xforms:instance xmlns="" id='loan'>

           <Loan>

               <Borrower>

               <Name>John Q. Public</Name>

               <Addr>123 Main St. Tinyville</Addr>

               </Borrower>

               <Principal currency="CDN">10000</Principal>

               <Duration>12</Duration>

               <InterestRate>5</InterestRate>

               <Payment>856.07</Payment>

               <TotalPayout>10272.84</TotalPayout>

           </Loan>

      </xforms:instance>



       <!-- More XForms instances for extra data -->

       <!-- XForms binds for model item properties

                and calculations -->



       <xforms:submission id='loanSub' method='post'

                            action='http://example.org'/>

</xforms:model>





Listing Two



<xforms:bind nodeset="Duration"

               type="xsd:nonNegativeInteger"/>

<xforms:bind nodeset="Principal | InterestRate"

               type="xsd:double" constraint=". > 0"/>

<xforms:bind nodeset="Borrower/*" required="true()"/>

<xforms:bind nodeset="Payment | TotalPayout"

               relevant="../Principal > 0 and

                          ../Duration > 0 and

                          ../InterestRate > 0"/>





Listing Three



<xforms:bind nodeset="TotalPayout"

               calculate="../Payment * ../Duration"/>

<xforms:instance id="rate" xmlns="">

      <rate></rate>

</xforms:instance>



<xforms:bind nodeset="instance('rate')"

               calculate="instance('loan')/InterestRate

                           div 100.0 div 12.0"/>

<xforms:bind nodeset="Payment"

               calculate="

                   if(instance('rate') > 0,

                      ../Principal * instance('rate')

                       div 

                       (1.0 - power(1.0 + instance('rate'),

                                    -../Duration))),

                       ../Principal * ../Duration"/>





Listing Four



<xforms:input ref="Principal">

        <xforms:label>Enter principal:</xforms:label>

</xforms:input>



<xforms:input ref="Duration">

        <xforms:label>Enter duration:</xforms:label>

</xforms:input>



<xforms:input ref="InterestRate">

        <xforms:label>Enter interest rate:</xforms:label>

</xforms:input>



<xforms:output ref="Payment">

        <xforms:label>Monthly payment:</xforms:label>

</xforms:output>



<xforms:output ref="TotalPayout">

        <xforms:label>Total to be repaid:</xforms:label>

</xforms:output>



<xforms:trigger>

       <xforms:label>Clear</xforms:label>

       <xforms:action ev:event="DOMActivate">

               <xforms:setvalue ref="Principal" value="'0'"/>

               <xforms:setvalue ref="Duration" value="'0'"/>

               <xforms:setvalue ref="InterestRate" value="'0'"/>

       </xforms:action>

</xforms:trigger>



<xforms:submit submission=`loanSub'>

       <xforms:label>Submit</xforms:label>

</xforms:submit>



Listing Five



<!-- Another instance in the XForms model -->



<xforms:instance id="curr" xmlns="">

       <currencies>

               <choice type="CDN"/>

               <choice type="Euro"/>

               <choice type="USD"/>

               <choice type="Yen"/>

       </currencies>

</xforms:instance>



<!-- In the user interface layer -->



<xforms:select1 ref="Principal/@currency">

        <xforms:label>Choose currency</xforms:label>

        <xforms:itemset nodeset="instance('curr')/choice">

                  <xforms:label ref="@type"/>

                  <xforms:value ref="@type"/>

        </xforms:itemset>

</xforms:select1>











3



