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

Home > Articles > Web Design & Development

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

Counting Nodes

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

Figure 4.8 Based on the XSLT from Figure 4.7, more than half the wonders were destroyed by earthquake.

To count nodes:

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