Skip to Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

Name

count() Function — Counts the number of nodes in a given node-set.

Synopsis

number count(
               node-set
               )

Inputs

A node-set.

Output

The number of nodes in the node-set.

Defined in

XPath section 4.1, Node Set Functions.

Examples

Here’s the XML document we’ll use to illustrate the count() function:

<?xml version="1.0"?>
<test>
  <p>This is a test XML document used by 
  several of our sample stylesheets.</p>
  <question>
    <text>When completed, the Eiffel Tower was the 
    tallest building in the world.</text>
    <true>You're correct!  The Eiffel Tower was the 
    world's tallest building until 1930.</true>
    <false>No, the Eiffel Tower was the world's 
    tallest building for over 30 years.</false>
  </question>
  <question>
    <text>New York's Empire State Building knocked 
    the Eiffel Tower from its pedestal.</text>
    <true>No, that's not correct.</true>
    <false>Correct!  New York's Chrysler Building, 
    completed in 1930, became the world's tallest.</false>
  </question>
</test>

Here’s a stylesheet that illustrates the count() function:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:text>Tests of the count() function:</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> count(/test)=</xsl:text> <xsl:value-of select="count(/test)"/> <xsl:value-of select="$newline"/> <xsl:text> count(/true)=</xsl:text> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano

Publisher Resources

ISBN: 0596000537Supplemental ContentCatalog PageErrata