XForms - Attribute Value Templates

Introduction

Certain attributes in XForms are literal values defined by the form author at the time the form is written, as opposed to being evaluated at runtime. Examples include the resource attribute on <xforms:submission> or <xforms:load>.

To improve this, Orbeon Forms supports a notation called Attribute Value Templates (AVTs) which allows including XPath expressions within attributes. You include XPath expressions in attributes by enclosing them within curly brackets ({ and }).

NOTE: AVTs were first introduced in XSLT.

Example

Consider this example:

<xforms:load resource="/forms/detail/{instance('documents-instance')/documents/document[index('documents-repeat')]/id}"/>

When <xforms:load> is executed, the resource attribute is evaluated. The results is the concatenation of /forms/detail/ and of the result of the expression within brackets:

instance('documents-instance')/documents/document[index('documents-repeat')]/id

If the id element pointed to contains the string C728595E0E43A8BF50D8DED9F196A582, the resource attribute takes the value:

/forms/detail/C728595E0E43A8BF50D8DED9F196A582

Note the following:

  • If you need curly brackets as literal values instead of enclosing an XPath expression, escape them using double brackets ({{ and }}).
  • You can use as many XPath expressions as you want within a single attributes, each of them enclosed by curly brackets.

AVTs on XForms elements

Model

<xforms:submission>

  • method
  • action and resource
  • serialization
  • mediatype
  • version
  • encoding
  • separator
  • indent
  • omit-xml-declaration
  • standalone
  • validate
  • relevant
  • mode
  • xxforms:target
  • xxforms:username
  • xxforms:password
  • xxforms:readonly
  • xxforms:shared
  • xxforms:xinclude
  • xxforms:calculate [SINCE: 2010-06-16]

Actions

<xforms:dispatch>

  • name
  • target
  • bubbles
  • cancelable
  • delay
  • xxforms:show-progress
  • xxforms:progress-message

<xxforms:hide>

  • dialog

<xforms:insert>

  • position

<xforms:load>

  • resource
  • replace
  • xxforms:target
  • xxforms:show-progress
  • f:url-type

<xforms:setfocus>

  • control
  • xxforms:deferred-updates

<xforms:setindex>

  • xxforms:deferred-updates

<xxforms:show>

  • dialog
  • neighbor
  • constrain

<xforms:toggle>

  • case
  • xxforms:deferred-updates

Controls

All controls

  • style: equivalent to the HTML style attribute
  • class: equivalent to the HTML class attribute

<xxforms:input> and <xxforms:secret>

  • xxforms:size: equivalent to the HTML size attribute
  • xxforms:maxlength: equivalent to the HTML maxlength attribute
  • xxforms:autocomplete: equivalent to the HTML autocomplete attribute

<xxforms:textarea>

  • xxforms:cols: equivalent to the HTML cols attribute
  • xxforms:rows: equivalent to the HTML rows attribute
  • xxforms:maxlength: equivalent to the HTML 5 rows attribute (NOTE: this HTML 5 attribute is not supported by most browsers as of March 2009)

AVTs on XHTML Elements

AVTs are also supported on XHTML elements. 

For example:

<xhtml:table class="zebra-table">
    <xhtml:tbody>
        <xforms:repeat nodeset="*">
            <xhtml:tr class="zebra-row-{if (position() mod 2 = 0) then 'even' else 'odd'}">
                <xhtml:td>
                    <xforms:output value="."/>
                </xhtml:td>
            </xhtml:tr>
        </xforms:repeat>
    </xhtml:tbody>
</xhtml:table>

In the above example, the value of the class attribute on <xhtml:tr> is determined dynamically through XPath and XForms. Even table rows get the class zebra-row-even and odd table rows get the class zebra-row-odd.

The values of XHTML attributes built using AVTs update as you interact with the XForms page. In the example above, inserting or deleting table rows after the page is loaded will still correctly update the class attribute.

It is also possible to use AVTs outside <xhtml:body>, for example:

<xhtml:html lang="{instance('language-instance')}" xml:lang="{instance('language-instance')}">...</xhtml:html>

AVTs are also usable on HTML elements within <xforms:label><xforms:hint><xforms:help><xforms:alert>:

<xforms:input ref="foobar">
    <xforms:label>
        <xhtml:span class="{if (. = 'green') then 'green' else 'red'}-label">Inverted label</xhtml:span>
    </xforms:label>
</xforms:input>

NOTE: It is not possible to use AVTs within the id attribute of XHTML elements.

Comments