Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
This chapter is from the book

Totaling Values

You can use the XPath sum() function to add up all the values of the nodes in a node set (Figure 4.20). It’s great for displaying data in tables and columns (Figure 4.21).

Figure 4.20

Figure 4.20 The XSLT in Figure 4.18 capitalized each wonder’s name as a header in the History section.

x s l t
 <td valign="top" align="right"
   colspan="4">Average Height: </td>
 <td valign="top">
  <xsl:value-of select="
  '##0.0')" />

Figure 4.21. I’m creating a row to show the average height of all the wonders. To find the average, I use the sum() function to add up all the values of the height nodes. Then I divide the total height by the number of wonders which don’t have a height of zero. Finally, I format the output to show one value to the right of the decimal point.

Figure 4.22

Figure 4.22 The average height of all the wonders was nearly 20 stories high.

To total values:

  1. Type sum(.
  2. Then, type the path to the node set whose nodes should be totaled.
  3. Finally, type ) to complete the function.
  • + Share This
  • 🔖 Save To Your Account