Often, rather than doing arithmetic on a set of nodes, you may simply want to know how many nodes there are (Figure 4.7).
x s l t ... Of these wonders, <xsl:value-of select=" count(ancient_wonders/wonder/ history/how_destroyed[. = 'earthquake'])" /> were destroyed by earthquake, <xsl:value-of select=" count(//how_destroyed[. = 'fire'])"/> were destroyed by fire, and <xsl:value-of select=" count(//wonder) - count(//how_destroyed)" /> is still standing. ...
Figure 4.7. In the first count() function I am using an absolute location path to return the number of wonders that were destroyed in an earthquake. In the last two, I used the // shortcut to return all nodes in the entire document that match the expression.
Figure 4.8 Based on the XSLT from Figure 4.7, more than half the wonders were destroyed by earthquake.
To count nodes:
- Type count(.
- Then, type the path to the node set whose nodes should be counted.
- Finally, type ) to complete the function.