Using Auto-Calculations Expand/Collapse All Print this Topic Previous Page Up One Level Next page

Home >  Quick Start Tutorial >

Using Auto-Calculations

Auto-Calculations are a powerful mechanism for providing additional information from the available XML data. In this section you will add two pieces of information to the design: the total number of news items and the time period covered by the news items in the XML document. Neither piece of information is directly available in the XML document but has to be calculated or manipulated from the available data.


Counting the news item nodes

In the design, do the following:


1.Create space, as shown in the screenshot below, for a line of static text (on which the Auto-Calculation will also be placed). Use the Return key to add new lines and insert a horizontal line below the space you create (see screenshot).




2.Type in the static text "Total number of news items: " as shown in the screenshot above.
3.Apply local styling of your choice to the static text. Do this as described in the section Formatting the Content.
4.Place the cursor after the colon and select Insert | Auto-Calculation | Value. This pops up the Edit XPath Expression dialog (screenshot below). (Alternatively, you can right-click and select the command in the context menu.)




5.In the schema tree, note that the context node is newsitems, which is highlighted. Now, in the Expression text box either type in the expression count(newsitem) or build the expression using the entry-helper panes below the Expression text box. (Double-click the count function (found in the Sequence group of functions) to enter it, then (in the expression in the text box) place the cursor within the parentheses of the function and double-click the newsitem node in the schema tree. You can see what the XPath expression returns by clicking the Evaluator button. The result of the evaluation will be in the Results pane (see screenshot below). For a detailed description of the Edit XPath Expression dialog, see the section Edit XPath Expression.




6.Click OK to finish. The Auto-Calculation is inserted in the design at  the cursor location (screenshot below). Format the Auto-Calculation using local styles.




Your HTML output will look like this:




Displaying the period covered by news items

The period covered by all the news items together can be obtained by getting the date of the earliest news item and the date of the latest news item. This can be achieved with XPath expressions like those given below. The first expression below outputs the contents of the date node. The second expression is a refinement, outputting just the month and year values in the date node. You can use either of these.


concat(min(//date), ' to ', max(//date)).
concat(month-from-date(min(//date)), '/', year-from-date(min(//date)), ' to ', month-from-date(max(//date)), '/', year-from-date(max(//date)))


In the design, insert the static text and Auto-Calculation as shown in the screenshot below. Apply whatever local styling you like.




The HTML preview will look something like this:




After you are done, save the file.




© 2019 Altova GmbH