This document presents each of the attributes used in templates to annotate the structure of the XML documents being presented and to modify the final output of the presented document.
The annotation attributes in this section are the most basic of those available. Apart from template:element
and template:i18n
, it is usually preferable to use the annotations listed in the "Convenience Annotations" section below.
This
attribute associates the template element on which it is used with an
element from the XML document being presented. Matching elements are
found from the current position (or context), where the position
is updated upon entering a template element with a template:element
or template:attribute
annotation.
Example:
<p template:element="item">
For each item element found, this section is produced.
<span template:element="subitem">
For each subitem element found within an item element, this is produced.
</span>
<span template:element="other">
This is produced for each other element found within an item element.
</span>
</p>
Example:
<p template:element="first,second,third">
For each third element found, this section is produced.
</p>
Syntax:
element-name1[,element-name2[,...]]
Related attributes:
template:init
This attribute associates the template element on which it is used with an attribute in the XML document being presented.
Example:
<p template:element="item">
<span template:attribute="value">
This section is associated with the value attribute of the item element.
To be really useful, we should also use other annotations to produce the value of the attribute.
</span>
</p>
Syntax:
attribute-name
Related attributes:
template:value
template:attribute-field
template:attribute-button
template:attribute-area
template:attribute-list-button
This attribute produces, in the final output, the value of part of the XML document being presented.
Example:
<p template:element="item">
<span template:attribute="value" template:value="$this-value">
This text will be replaced by the value of the attribute, but the span template element will remain.
</span>
</p>
Example:
<p template:element="item">
<span template:attribute="value" template:value="$this-value" template:effect="replace">
This text and its enclosing span element will be replaced by the value of the attribute.
</span>
</p>
Syntax:
XPath-expression
Here, the underlying XPath mechanisms are exposed, but a number of useful shortcuts are available:
$this-value
produces the value of the current position (or context) in the XML document being presented.template:this-attribute()
produces a reference to the current attribute (or context).Related attributes:
template:effect
This attribute modifies the effect of a template:value
annotation.
Example:
(See above.)
Syntax:
insert|replace
Related attributes:
template:effect
template:attribute-area
This attribute permits the inclusion of a section of the template document according to a test performed on the XML document being presented.
Example:
<p template:if="@value = 'true'">
If the value attribute is set to the string value 'true', include this section.
</p>
Syntax:
XPath-expression
Here, the underlying XPath mechanisms are exposed, and any XPath expression which tests aspects of the XML document can be written.
This attribute is used to translate the textual contents of an element to another language where additional parameters specifying the language and the whereabouts of the translations have been provided to the stylesheet in the output generation process.
Example:
<span template:i18n="-">Hello</span>
In this example, the contents of the span
element would be replaced with an appropriate translation for the text Hello
.
Example:
<span template:i18n="hello">Hello</span>
In this example, the contents of the span
element would be replaced with an appropriate translation using the token hello
as a key in the translation dictionary.
Example:
<span template:i18n="{$this-value}">Hello</span>
In this example, the value of the special template variable $this-value
is used as a key in the translation dictionary.
Syntax:
-|{expression}|token
See the "Internationalisation" document for more information on this attribute.
The annotation attributes in this section control the initialisation of documents where this is done by the XSLForms toolkit.
This attribute controls the creation of elements in the initialisation process and is used together with template:element
. For each element name listed in a template:element
annotation, the corresponding value in a template:init
annotation states whether or how such elements are to be initialised.
Example:
<p template:element="item" template:init="no">
Such item elements will not be created automatically when the document is initialised.
</p>
Example:
<p template:element="first,second,third" template:init="yes,yes,no">
The first and second elements will be created automatically when the document is initialised.
No third elements will be created automatically when the document is initialised.
</p>
Syntax:
yes|no|auto[,yes|no|auto[,...]]
Here, yes
means that an element will be created automatically, no
means that an element will not be created automatically (relying on the existence of such elements from before), and auto
means that the initialisation process will attempt to guess whether an
element should be created automatically (by looking for selectors which
use the element's name and only creating elements where no such
selectors could be found). Where no values are provided, auto
is assumed.
Related attributes:
template:element
The annotation attributes in this section provide more convenient ways of presenting the XML document information in the final output.
This attribute associates the template element on which it is used with an attribute from the XML document, whilst providing certain other attributes in the final output:
name
attribute is used to identify the location of the attribute in the XML document being presented.value
attribute is used to present the value of the attribute from the XML document.Example:
<input template:attribute-field="name" name="..." value="..." type="text"/>
This would be output as follows:
<input name="path-to-name" value="value-of-name" type="text"/>
...where path-to-name
and value-of-name
would be replaced with the appropriate computed values.
Syntax:
attribute-name
Related attributes:
template:attribute-area
template:attribute-button
Implementing attributes:
template:attribute
This
attribute associates the template element on which it is used with an
attribute from the XML document being presented, much in the way
that template:attribute-field
does. However, the
attribute value is not inserted into an attribute in the final output;
instead it is inserted into the output in a different way according to
additional information specified in the annotation.
Example:
<textarea template:attribute-area="name" name="..." cols="40" rows="5">
This text will be replaced by the value of the name attribute from the document being presented.
The textarea element will enclose the attribute value in the final output.
</textarea>
Example:
<p>
The name is:
<span template:attribute-area="name,replace">
This text will be replaced in the final output, and the span element will not be reproduced.
</span>
</p>
Syntax:
attribute-name[,insert|replace]
By default, the value of attribute-name
is inserted within the template element on which the annotation appears, and this is equivalent to specifying insert
. If replace
is specified, the template element is entirely replaced by the value of attribute-name
.
Related attributes:
template:attribute-field
Implementing attributes:
template:attribute
This attribute associates the template element on which it is used
with an attribute from the XML document, whilst providing certain other
attributes in the final output, much like template:attribute-field
does, but with features which make the presentation of buttons and related user interface controls much simpler:
name
attribute is used to identify the location of the attribute in the XML document being presented.value
attribute is used to present a specified value associated with the button being activated.Example:
<input template:attribute-button="question-type,text,checked" name="..." value="..." type="radio"/>
This would produce a "radio" button like the following:
<input name="path-to-question-type" value="text" type="radio"/>
If the value of question-type
was set to text, output like the following would be produced:
<input name="path-to-question-type" value="text" type="radio" checked="checked"/>
In an XHTML document, this would cause the "radio" button to appear selected or activated.
Syntax:
attribute-name,attribute-value,attribute-to-create-when-set
Related attributes:
template:attribute-field
template:attribute-list-button
Implementing attributes:
template:attribute
template:expr
template:expr-attr
This attribute permits the definition of references to the parts of the XML document being presented which are associated with the template elements in which it is used. When used in certain HTML form-related elements, it becomes possible to receive and obtain such references from the XSLForms toolkit and to then access the selected parts of the original document.
Example:
<p template:element="item">
<input template:selector-field="remove" name="..." value="Remove this item" type="submit"/>
</p>
Example:
<p template:element="item">
An item...
</p>
<input template:selector-field="add,item" name="..." value="Add item" type="submit"/>
Syntax:
selector-name[,element-name]
The optional element name can be used to associate the selector with other elements; when this is done, the initialisation of the XML document will be affected such that the named element will not be automatically created in the initialisation process, since the presence of the selector implies that such elements can be added and removed in the application user interface.
Related attributes:
template:element
The annotation attributes in this section provide ways of presenting enumerations and selections of values.
This attribute associates the template element on which it is used with an attribute in the XML document being presented whose value is to be chosen from a list of possibilities. The list itself is represented by a list of elements, each having an attribute with a distinct value; such information is added in the initialisation of the document before it is presented, since it is not part of the "essential" information in the document.
Example:
<select template:multiple-choice-field="base-system,value" name="...">
...
</select>
Example:
<select template:multiple-choice-field="-,question-type" name="...">
...
</select>
See below for an example combining this attribute with the template:multiple-choice-value
attribute.
Syntax:
element-name|-,attribute-name[,new|,new,dynamic|,,dynamic]
Where the special value -
is given as the element name, the context element is chosen as the
element in the XML document being presented whose attribute is
involved. Where the optional parameter new
is given, the attribute is assumed not to already exist on the element. Where the optional parameter dynamic
is given, the source of the multiple-choice values is considered to be found dynamically rather than from another document.
Related attributes:
template:multiple-choice-value
template:multiple-choice-list-field
Implementing attributes:
template:element
template:attribute
This attribute associates the template element on which it is used with an element in the XML document being presented whose purpose is to hold one of a list of selectable values (as described above).
Example:
<select template:multiple-choice-field="base-system,value" name="...">
<option template:multiple-choice-value="base-system-enum,value,selected" value="..."/>
</select>
This presents the following document fragment:
<base-system value="c">
<base-system-enum value="a"/>
<base-system-enum value="b"/>
<base-system-enum value="c"/>
</base-system>
The output from the combination of the above would be as follows:
<select name="path-to-base-system">
<option value="a">a</option>
<option value="b">b</option>
<option value="c" selected="selected">c</option>
</select>
Labels different from the actual values employed can be specified using an extra parameter in the annotation:
<select template:multiple-choice-field="base-system,value" name="...">
<option template:multiple-choice-value="base-system-enum,value,selected,text()" value="..."/>
</select>
This
additional parameter is an XPath expression whose context is the
current value-bearing element. The above example selects the text from
inside each base-system-enum
element in the modified document fragment presented below.
A modified document fragment providing the labels can be written as follows:
<base-system value="c">
<base-system-enum value="a">A</base-system-enum>
<base-system-enum value="b">B</base-system-enum>
<base-system-enum value="c">C</base-system-enum>
</base-system>
The output from the modified combination would be as follows:
<select name="path-to-base-system">
<option value="a">A</option>
<option value="b">B</option>
<option value="c" selected="selected">C</option>
</select>
Syntax:
list-element-name,list-attribute-name,attribute-to-create-when-selected[,content-expression]
Related attributes:
template:multiple-choice-field
template:multiple-choice-list-value
Implementing attributes:
template:element
template:expr
template:expr-attr
template:value
This attribute is similar to template:multiple-choice-field
except that it associates the template element on which it is used with
an element in the XML document being presented containing a list
of elements whose values have been selected. This list of selected
elements is expanded before presentation to include elements whose
values have not been selected. Consequently, the selected elements are
marked in a particular way to distinguish them from the non-selected
elements.
Example:
<select template:multiple-choice-list-field="question-types,question-type-enum,question-type" name="..." multiple="multiple">
...
</select>
See below for an example combining this attribute with the template:multiple-choice-list-value
attribute.
Syntax:
element-name|-,list-element-name,list-attribute-name[,dynamic]
Here, element-name
is the element in the document being presented which contains the selected value elements; if the special value -
is given then the context element is the element containing the selected value elements. The list-element-name
and list-attribute-name
indicate the details of the elements providing the list of selectable values. Where the optional parameter dynamic
is given, the source of the multiple-choice values is considered to be found dynamically rather than from another document.
Related attributes:
template:multiple-choice-list-value
template:multiple-choice-list-element
template:multiple-choice-field
Implementing attributes:
template:element
This attribute is similar to template:multiple-choice-value
in that it associates the template element on which it is used with an
element holding a value in a list of selectable values, with the
principal difference that potentially many such values may be selected
in this case.
Example:
<select template:multiple-choice-list-field="question-types,question-type-enum,question-type" multiple="multiple">
<option template:multiple-choice-list-value="question-type-enum,question-type,selected" value="..."/>
</select>
This presents the following document fragment:
<question-types>
<question-type-enum question-type="text"/>
<question-type-enum question-type="choice" value-is-set="true"/>
<question-type-enum question-type="special" value-is-set="true"/>
</question-types>
The output from the combination of the above would be as follows:
<select name="path-to-question-types" multiple="multiple">
<option value="text">text</option>
<option value="choice" selected="selected">choice</option>
<option value="special" selected="selected">special</option>
</select>
Labels different from the actual values employed can be specified using an extra parameter in the annotation:
<select template:multiple-choice-list-field="question-types,question-type-enum,question-type" multiple="multiple">
<option template:multiple-choice-list-value="question-type-enum,question-type,selected,text()" value="..."/>
</select>
This additional parameter is an XPath expression whose context is
the current value-bearing element. The above example selects the text
from inside each base-system-enum
element in the modified document fragment presented below.
A modified document fragment providing the labels can be written as follows:
<question-types>
<question-type-enum question-type="text">Text</question-type-enum>
<question-type-enum question-type="choice" value-is-set="true">Choice</question-type-enum>
<question-type-enum question-type="special" value-is-set="true">Special</question-type-enum>
</question-types>
The output from the modified combination would be as follows:
<select name="path-to-question-types" multiple="multiple">
<option value="text">Text</option>
<option value="choice" selected="selected">Choice</option>
<option value="special" selected="selected">Special</option>
</select>
Syntax:
list-element-name,list-attribute-name,attribute-to-create-when-selected[,content-expression]
Related attributes:
template:multiple-choice-list-field
template:multiple-choice-value
Implementing attributes:
template:element
template:expr
template:expr-attr
template:value
This attribute works in much the same way as template:multiple-choice-list-field
except that it does not add a name
attribute to the template element on which it is used. The purpose of this attribute, along with template:attribute-list-button
, is to provide an alternative approach to presenting lists of selectable values.
Example:
<p template:multiple-choice-list-element="question,question-types,question-type">
...
</p>
See below for an example combining this attribute with the template:attribute-list-button
attribute.
Syntax:
element-name|-,list-element-name,list-attribute-name
Here, element-name
is the element in the document being presented which contains the selected value elements; if the special value -
is given then the context element is the element containing the selected value elements. The list-element-name
and list-attribute-name
indicate the details of the elements providing the list of selectable values.
Related attributes:
template:multiple-choice-list-field
template:attribute-list-button
Implementing attributes:
template:element
This attribute works in much the same way as template:attribute-button
does, but instead presents a button or related user interface control
whose state reflects the presence of an attribute on an element in a
collection of elements.
Example:
<p template:multiple-choice-list-element="question,question-types,question-type">
<input template:attribute-list-button="question-type,checked" name="..." value="..." type="checkbox"/>
</p>
Given an XML document like this...
<question>
<question-types question-type="text" value-is-set="true"/>
<question-types question-type="choice"/>
</question>
...the following would be produced as output:
<p>
<input name="path-to-question-type" value="text" type="checkbox" checked="checked"/>
</p>
<p>
<input name="path-to-question-type" value="choice" type="checkbox"/>
</p>
Syntax:
attribute-name,attribute-to-create-when-set
Related attributes:
template:multiple-choice-list-element
template:attribute-button
Implementing attributes:
template:attribute
template:expr
template:expr-attr
The special annotations template:expr
and template:expr-attr
are used respectively to test an XPath expression in the current
context and to provide the name of an attribute which is to be created
in the final output, should the expression evaluate to a true value.
In order to support explicit, additional namespaces in output document processing, the special expr-prefix
attribute can be used to ensure the presence of a namespace declaration
in the generated stylesheet files. Consider the usage of various
stylesheet extension functions:
<body>
<p>
<span template:value="sum(str:split('1 2 3 4 5'))">result</span>
</p>
</body>
For the stylesheet which produces the output, the str
namespace will need to be declared somewhere. To do this, we must not
only declare the namespace in the template document, but we must also
provide a hint to the stylesheet processor that the namespace is
important (even though it is only mentioned in the text of an
attribute, which might persuade some XML processing toolkits to abandon
the namespace declaration).
<body xmlns:str="http://exslt.org/strings" str:expr-prefix="str http://exslt.org/strings">
<p>
<span template:value="sum(str:split('1 2 3 4 5'))">result</span>
</p>
</body>
What the special expr-prefix
attribute does is to provide that hint to the stylesheet processor,
although XSLTools also uses the attribute for other internal purposes.
Consequently, the resulting output stylesheet is able to find the str:split
extension function and to evaluate the expected result.
One important application of the expr-prefix
attribute is the restoration of damaged attributes after the editing of
a template document using a less than careful HTML editor. Consider the
above document fragment after a small edit in Nvu:
<body expr-prefix="str http://exslt.org/strings" str="http://exslt.org/strings">
<p>
<span value="sum(str:split('1 2 3 4 5'))">result!</span>
</p>
</body>
Nvu has obviously decided to remove the str
and template
namespace prefixes. To revert this damage, a tool is provided with XSLTools called xslform_fix.py
(found in the scripts
directory in the source distribution), and this tool will attempt to re-add namespace declarations for template
automatically (since the template
namespace is obviously the basis of XSLForms) along with any namespaces declared using expr-prefix
attributes. The tool is invoked as follows:
xslform_fix.py template.xhtml template_fixed.xhtml
Since namespace declarations may be moved to the root element of the new template document, it is possible that some problems may result with documents using namespaces in advanced ways, but the tool should provide a reasonable solution for most documents.