Variable
The variable Element
The variable element introduces a variable to be used in XPath expression in the subtree of the parent to the variable element.
Attributes
name |
Required |
The name of the variable. This is a xsd:QName. |
select |
Required |
[XPath 1.0] expression which is the value assigned to the variable. The expression is evaluated in the context defined by [XForms 1.0] and by the model attribute on this element. |
model |
Optional |
The xforms model used for scoping which model the attribute correspons to. |
For variables in the model they are updated before the processing of the xforms-recalculate event and for variable elements in the UI they are updated before the xforms-refresh event. A variable scope is defined in a model and from the parent of the variable element and the last one with the same name takes precedens. It must be possible to defined identical named variables for different model in the same document scope.
Erik's Notes
- Variables must also be defined within actions, wherever these actions take place.
- Scoping of variables must be documented. E.g. do you see, from the view, model variables? If so, how does scoping change when you switch models with @model? Etc.
Example of variable in actions (here just using a variable for convenience, you can actually get by with context()):
<xforms:action iterate="a | b | c | d">
<xforms:variable name="local-name" select="local-name()"/>
<xforms:setvalue ref="@id" value="concat('id-', $local-name)"/>
</xforms:action>
Example: how to use variable in the UI
<xforms:model>
<xforms:instance>
<data xmlns="">
<a><b><c>value</c></b></a>
</data>
</xforms:instance>
</xforms:model>
<xforms:group>
<xforms:variable name="bvalue" select="a/b"/>
The c value: <xforms:output ref="$bvalue/c"/>
</xforms:group>
Example: how to use variable in the model
<xforms:model>
<xforms:instance>
<data xmlns="">
<a><b><c>value</c></b></a>
</data>
</xforms:instance>
<xforms:variable name="bnode" select="a/b"/>
<xforms:variable name="boolean" select="$bnode/c = 'value'"/>
<xforms:bind nodeset="$bnode" relevant="$boolean"/>
</xforms:model>
More of Erik's notes about variables related to the proposed @name attribute
Erik needs to clean this up
- @name is introduced for 2 reasons:
- solve scoping issues with @id
- closer from HTML forms syntax in streamlined syntax in UI
- @name should replace/add to @id in model AND view
- model, instance, submission, bind
- controls
- @name is scoped by model in model AND view
- @name and variable creation
- does NOT automatically create variables
- in streamlined, creates implicit xf:bind AND xf:variable
- so only xf:variable actually create a variable
- @name can be used in UI alonside @bind, @ref and @nodeset
- in that case, it does not cause the creation of a variable
-> RATIONALE: there is already instance data, so no need for creation of a variable to access value
- in that case, it does not cause the creation of a variable