Data templates and transformations > Data transformations > Using local variables in transformations

Using local variables in transformations
You can use local transformation variables in order to keep values that you plan to reuse later. For example, if calculation of a value takes time it might be more efficient to create a transformation variable for the value and then reuse this variable. Another scenario when transformation variables should be used is described in Example – using a variable to access lookup data.
The following example illustrates a simple usage scenario where a text node will contain the string Hello World in the output.
 
$hello
'Hello'
$world
'World'
$greeting
concat($hello, ' ', $world)
text()
$greeting
Variable evaluation
Local transformation variables are evaluated in declaration order. In the example above, the variables are evaluated in this order: $hello - $world - $greeting. If the order in the example would be $greeting - $hello - $world then the $greeting variable would not be evaluated correctly.
When you define a variable for an element, its value can be used in the element and in all its descendants (not in siblings or parent elements). You can re-define the variable on each descendant level, but you can only define it once at each level. If you re-define the variable on a child element, the new value is used in this scope. Outside this scope, the value of the parent is used.
The following examples illustrate incorrect and correct definitions of variables (for visibility, the syntax described in Data transformation persistence is used).
Example 17
<tdt:rule path="/data/sentence">
<tdt:value key="$address">'http://'</tdt:value>
<tdt:value key="$address">concat($address, 'example.com')
</tdt:value>
</tdt:rule>
 
Example 18
<tdt:rule path="/data">
<tdt:value key="$address">'http://'</tdt:value>
</tdt:rule>
<tdt:rule path="/data/sentence">
<tdt:value key="$address">concat($address, 'example.com')
</tdt:value>
</tdt:rule>
 
For repeated elements the variables are evaluated again and again for every iteration. In the following example the $position variable has values 1, 2, 3, ...
/data/message/sentence
$position
position()
Transformation variables are strictly typed
Transformation variables are strictly typed and can be of one of the following types:
The previous example with concat($hello, $world) works for the string variables $hello and $world. Using $hello | $world would fail since | is only defined for node-sets.
Note that StoryTeller variables accessed from presentation always are node-set, which means $hello | $world would work in presentation.
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26