Re: Counting nodes

Subject: Re: Counting nodes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Aug 2000 11:36:39 +0100
Gilles,

>I'd like to know if it possible to count the not empty <cell> node using
>xslt
>eg to have a result like this:

Just a bit of terminology confusion - an 'empty' element is usually one
that doesn't have any content, i.e. doesn't have anything between its start
and end tags, or, as in your case, has a single empty-element tag like:

  <cell type="start" name="Case0" pos="0"/>

To count how many nodes there are of a certain type, then you should use
the count() function with the argument being the nodes that you want to
count.  You want to select all the 'cell' elements for which a certain
condition is true (they have some attributes).  Where you want to select
nodes that fulfil a certain condition, you should use a predicate (a
boolean expression within []s).

So, to select all cells in your example that have some attributes, use:

  /board/row/cell[@*]

and to count them use:

  count(/board/row/cell[@*])

and to display the result use:

  <xsl:value-of select="count(/board/row/cell[@*])" />

One thing that may or may not be an issue in your case is that if you give
a DTD for your source XML, then any attributes that have a default value
defined within the DTD will count as attributes within your elements.  So
for example if you had a DTD given as:

<!DOCTYPE board [
<!ATTLIST cell
 type	(start|bp|qcm|luck) 'qcm'
 name  CDATA               'Case0'
 pos   CDATA               '1'>
]>

then the above would not work, and in fact it would be impossible to tell
which of your 'cell' elements actually had attributes defined within the
XML source and which just had defaulted values from the DTD.

I hope that this helps,

Jeni

Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • Counting nodes
    • Gilles Durys - Fri, 25 Aug 2000 10:10:22 +0200
      • Jeni Tennison - Fri, 25 Aug 2000 11:36:39 +0100 <=
      • <Possible follow-ups>
      • Ben Robb - Fri, 25 Aug 2000 09:18:54 +0100