[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Counting nodes

Subject: Re: Counting nodes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 25 Aug 2000 11:36:39 +0100
Re: Counting nodes
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.