To count all the *data* elements within the document- you can do something
like
<xsl:template match="/">
<xsl:variable name="total-el">
<xsl:value-of select="count(descendant::data)"/>
</xsl:variable>
</xsl:template>
To count all children - you can do count(descendant::*) - This will also
count <text> nodes.
To count all children directly under root/data - do count(*)
Now you have to compute the values of T and R
Ritu Kama
Sarvega Inc
745, McLintock Dr
Burr Ridge
IL - 60527
----- Original Message -----
From: "bix xslt" <bix_xslt(_at_)hotmail(_dot_)com>
To: <XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Tuesday, November 26, 2002 4:38 PM
Subject: [xsl] Counting Child Nodes
All,
As I've been developing my xsl scripts, I've realized that it would be
beneficial to be able to write my xslt in such a way that it would
dynamically format my page for an optimal view based on a few factors.
Essentially, I have a very large table which contains data from its child
node and its grandchild node. An example xml file would be:
<data><text>Title for web page</text>
<data><text>Motivation</text>
<data><text>White Pages</text></data>
<data><text>References</text></data>
</data>
<data><text>Requirements</text>
<data><text>Software</text></data>
<data><text>Hardware</text></data>
</data>
<data><text>Design</text>
<data><text>Interfaces</text>
<data><text>Hardware/Software</text></data>
<data><text>Legacy Software</text></data>
</data>
<data><text>Functions</text></data>
</data>
<data><text>Implementation</text></data>
<data><text>Verification</text></data>
<data><text>Documentation</text></data>
<data><text>Archive</text></data>
</data>
Note that there are 7 <data> elements that are children of the root
node. In my table, I could display each one in their own row or column,
but
I would prefer to put three children on the first and second lines, and
then
a single child on the third line. An example of this would be:
One per column:
[Motivation] [Requirements] [Design] [Implementation]
.
.. . .
One per row:
[Motivation]
[Requirements]
[Design]
.
.
Optimal View:
[Motivation] [Requirements] [Design]
[Implementation] [Verification] [Documentation]
[Archives]
The algorithm I would use to determine how many rows and columns would
be as follows:
T = Total number of children
R = Number of children displayed per row
C = Number of columns needed
<!-- find T -->
T = ??
<!-- initialize the values -->
set R = 1
set C = T
<!-- setup a loop to find the best numbers -->
while ( R < C and C > 1 )
R = R + 1
C = C - 1
<!-- numbers should now be close to optimal, so start displaying -->
So, my question is two-fold: How do I find T, and is there a slick
way
of converting the psuedo code above into XSLT?
Thanks,
Bix
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list