Using and storing variables

by Peter on September 30, 2009

in User Reference

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>

GetVn

With Version 4 of XpressDox, the function <<GetVn()>> was introduced – it is needed in situations where the value of a variable is known to be numeric and is required as such by the particular command being used.  Examples would be <<substring(FirstNames,GetVn(‘count’),1)>> or <<Party[GetVn(‘Counter’)]/Name>>.

SetVr

Often a SetV function is used but the template author wants the paragraph in which it is used to be removed.  The RemoveParagraph function can be used for this, but the SetVr function provides a shorthand way of performing the SetV and then removing the paragraph.

Related posts

Leave a Comment