In the "Template Design" document, the special XSLForms annotations were presented as a means to display the information contained in form data documents (or just general XML documents with no special connection to Web forms). However, since XSLForms templates build upon XSL transformations, it is also possible to employ certain XSL techniques to present the information in various different ways.
When prepared, XSLForms templates are converted to XSL stylesheets, and when form data is displayed, such XSL stylesheets are used to transform the form data to a representation which looks like the original template, but with different parts of the template populated with the form data. The process can be illustrated as follows:
Template A template presents the general form of the final output. |
Stylesheet Templates are converted to stylesheets, which are like programs specially designed to present XML information. |
Output The final output is produced when a stylesheet is combined with an XML document. |
---|
Since the stylesheet is based on XSL, which is a rich language in its own right, advanced template design techniques can involve some of the features of XSL - at least those which do not affect the simplicity or general structure of our templates.
One area where XSL features are already employed to affect the final output in XSLForms is in the toolkit's use of template extension functions and special variables - the latter being used in the output production process. For example, we might decide to use the lower level template functions to present the value of an attribute:
<span template:attribute="some-attribute" template:value="$this-value">xxx</span>
In the above example, we reference the special variable $this-value
which refers to the value of some-attribute
. An example of template functions in use looks like this:
<span id="{template:this-element()}">xxx</span>
In the above example, we invoke the template function template:this-element
in order to create a unique identifier in the final output. Since we
use the function inside an attribute which is not prefixed with template
, we must enclose the expression between {
and }
characters.
Since the above template extension functions and variables are merely special in the sense that XSLForms provides them to produce its output, and since they are accessed in the stylesheet using normal XSL-based mechanisms, there is no technical barrier to using other kinds of valid XSL (or more precisely, XPath) expressions in cases such as those given above. The rules for using such expressions in attributes are straightforward:
template
can contain expressions as one would write them normally.{
and }
characters so that the expression is evaluated and replaced with the result in the final output.Here is a trivial example of the usage of an XPath expression, based on one of the above examples:
<span template:attribute="some-attribute" template:value="string-length($this-value)">xxx</span>
In the above example, we invoke the standard XPath function string-length
in order to produce within the span
element the length of the value of some-attribute
(instead of the actual value).
More interesting applications of XPath expressions and non-XSLForms functions arise when using more of the potential of XPath to select arbitrary elements and attributes and to perform calculations on the selected nodes. The following example originates from the Configurator example application:
<span template:value="sum(/configuration//*[@value-is-set]/@price) + sum(/configuration//*[@value = ../@value]/@price)"></span>
This complicated expression, to be inserted within the span
element, finds all elements in the system configuration having the value-is-set
attribute and adds their prices together; this total is combined with
the sum of the prices from all elements marked as selected in their
respective lists. In other words, it gets the total of all selected
components and inserts it into the final output.