سؤال

I need to verify a condition when editing an XML document using XForms.

The complete sample web page is included below.

I would like to verify two things

  1. values inside 'v' elements are integers
  2. each value of v is lesser than value inside 'max' element (so the sample document should not pass validation, because the value inside /doc/values/rec[3] is 4, greater than 3 inside /doc/max.

I don't know how to set the attributes of xf:bind Using this:

<xf:bind id="bindv" 
         nodeset="instance('i1')/values/rec/v" 
         type="integer" />

and

<xf:input bind="bindv">

causes that xf:input only edits the first 'v'.

<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:ev="http://www.w3.org/2001/xml-events" 
      xmlns:xf="http://www.w3.org/2002/xforms">
    <head>        
        <xf:model>
            <xf:instance xmlns="" id="i1">
                <doc>
                    <max>3</max>
                    <values>
                        <rec>
                            <v>1</v>
                        </rec>
                        <rec>
                            <v>2</v>
                        </rec>
                        <rec>
                            <v>4</v>
                        </rec>
                    </values>
                </doc>
            </xf:instance>          
        </xf:model>     
    </head>
    <body>
        <div>
            <table>
                <thead>
                   <th>Value</th>                                        
                </thead>    
                <tbody id="values-repeat" 
                    xf:repeat-nodeset="instance('i1')/values/rec">
                    <tr>
                       <td>
                           <xf:output ref="v" />
                       </td>                       
                    </tr>
                </tbody>                  
            </table>  

            <xf:group ref="instance('i1')/values/rec[index('values-repeat')]" 
               appearance="full">               
                <xf:input bind="bindv">
                   <xf:label>v:</xf:label>
               </xf:input>
            </xf:group>
        </div>
    </body>
</html>
هل كانت مفيدة؟

المحلول

The xf:input control, like most XForms controls, performs what the spec calls 'single-node binding'. If it's bound to a nodeset containing multiple nodes, it binds to the first. (You've noticed this already.) The problem is not in the bind element, which is fine as far as it goes, but in the use of

<xf:input bind="bindv"/> 

when what you mean is probably something more like

<xf:group ref="instance('i1')/values/rec[index('values-repeat')]" 
          appearance="full">               
    <xf:input ref="v">
        <xf:label>v:</xf:label>
    </xf:input>
</xf:group>

When I change the group as shown above (and change the handling of tbody, since the XForms processor I'm working with doesn't support the xf:repeat-nodeset attribute), the form works as apparently intended: there are three values, and one input widget, and when I click on one of the values, that value appears in the input area.

Note that you don't have to bind the input widget to the bindv binding for the type information to take effect; the properties declared in the xf:bind element apply to the nodes in its nodeset, and when you bind an input widget to one of those nodes, it knows about those properties.

The ID on the binding could be used, if you like, on an xf:repeat. The tbody, for example, can be expressed this way:

<tbody>
    <xf:repeat bind="bindv" id="values-repeat" >
        <tr>
            <td>
                <xf:output ref="." />
            </td>                       
        </tr>
    </xf:repeat>
</tbody>

To add the constraint that the values should be strictly less than the value of instance('i1')/max, you can make the xf:bind say something like this:

<xf:bind id="bindv" 
         nodeset="instance('i1')/values/rec/v" 
         type="integer"
         constraint=". &lt; ../../../max"
         />

Note that the context node for the evaluation of the XPath expression in the constraint attribute is a (or: the current) member of the nodeset.

When I add the constraint attribute, then an error signal appears against the third value when I load the document using an XForms processor.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top