This concept enables the results of calculations to be stored for further use in the template, and even for future templates.
Here are some examples to illustrate this, assuming a data set which looks like:
<xml>
<librarianName>Mrs. J. Bloggs</librarianName>
<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<costPrice>200</costPrice>
</book>
<book>
<title>Carrie</title>
<author>Stephen King</author>
<costPrice>150</costPrice>
</book>
</xml>
Setting, getting and saving a simple variable
<<SetV(‘TotalCost’,sum(book/costPrice))>> creates a variable named ‘TotalCost’ with a value of 350.
<<GetV(‘TotalCost’)>> inserts the value of the ‘TotalCost’ variable into the document.
<<SaveV(‘TotalCost’,’totalCost’)>> creates a data element named ‘totalCost’ as a direct descendant of the root element with the value of the ‘TotalCost’ variable. This only happens after the full template merge has completed, so the new data element will not be available in the template in which it is created.
A repeated variable
<<ForEach(book)>>
<<SetV(‘SellingPrice’,costPrice*1.5)>>
Cost Price: <<FormatNumber(costPrice,”#!,##0.00”)>>
Selling Price: <<FormatNumber(GetV(‘SellingPrice’),”#!,##0.00)>>
<<SaveV(‘SellingPrice’,concat(‘book[‘,position(),’]/sellingPrice’))>>
<<End(forEach)>>
At the end of the above two examples, the XML data set would look like this:
<xml>
<librarianName>Mrs. J. Bloggs</librarianName>
<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<costPrice>200</costPrice>
<sellingPrice>300</sellingPrice>
</book>
<book>
<title>Carrie</title>
<author>Stephen King</author>
<costPrice>150</costPrice>
<sellingPrice>225</sellingPrice>
</book>
<totalCost>350</totalCost>
</xml>